0

I want to find position start and end of pattern on my text.

I use following code:

Pattern _pat=Pattern.compile("20.  Eslaminejad MB, Nikmahzar A, Piriea A. The structure   of "
+"Human Mesenchymal Stem Cells differentiated into cartilage in micro mass culture   system. Yakhteh 2006, 8(3): "
+"162-171.");

Matcher _match=_pat.matcher("19.  Eslaminejad  MB,  Eftekhari-Yazdi  P.   Mesenchymal  stem "
 +"cells: In vitro differentiation among bone and cartilage cell "
+"lineages. Yakhteh. 2007; 9(3): 158-169."
+"20.  Eslaminejad MB, Nikmahzar A, Piriea A. The structure of "
+"Human Mesenchymal Stem Cells differentiated into cartilage in micro mass culture     system. Yakhteh 2006, 8(3): "
+"162-171."
+"21.  Pioletti  DP,  Montjovent  MO,  Zambelli  PY,  Applegate  L. "
+"Bone tissue engineering using foetal cell therapy. Swiss "
+"Med Wkly. 2006 ; 136(35-36): 557-560.");


    while(_match.find()){
       System.out.println(_match.start());
       System.out.println(_match.end());
    }

Unfortunately, the pattern is not finding the string in my text.

3
  • 3
    This isn't how you use regex. Please look at this page: regular-expressions.info/tutorial.html Commented Jul 8, 2013 at 8:32
  • 2
    In your search string you have more whitespace between words than you do in the text being searched. For example between "structure" and "of" Commented Jul 8, 2013 at 8:35
  • 2
    use regex only if you can't do it with normal string functions.. Commented Jul 8, 2013 at 8:36

2 Answers 2

2

Apparently, what you need is not regex, but just

int start = inputString.indexOf(stringToSearch);

This will give you the start position and the end position is calculated as

int end = start+stringToSearch.length();

You may want to normalize whitespace or perform other typical normalization operations before attempting a match.

Sign up to request clarification or add additional context in comments.

Comments

0

Your problem is in different number of spaces. Compare:

The structure   of

from the pattern with

The structure of

from the text.

By the way although you are using patterns you do not use any features that patterns provide, so in you case you could use simple String.contains() method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.