> Hello
>
> We have unfortunately the siituation that a regular expression could
> match mutliple times in a string. Currently it seems that it takes
> always the last.
I think you are mistaken. It doesn't do that for me (see below).
> I would like to know if this is "guaranteed" or
> somewehere specified that it takes "always" the "last" one.
The docs suggest it always matches the leftmost* one first.
* The matches method attempts to match the entire input sequence
against the pattern.
* The lookingAt method attempts to match the input sequence,
starting at the beginning, against the pattern.
* The find method scans the input sequence looking for the next
subsequence that matches the pattern.
> I now that
> I can use "^" or "$" to specify that more precisely but unfortunately
[quoted text clipped - 8 lines]
> Matcher m = p.matcher(text);
> boolean b = m.matches();
String text = "aaa1 bbb aaa2";
Pattern p = Pattern.compile("aaa\\d");
Matcher m = p.matcher(text);
m.find();
System.out.println(m.group());
Output: aaa1
* For western locales, I've no idea what it does for locales where text
is written right to left. Presumably it does what would be expected.
Daniel Pitts - 25 May 2007 18:57 GMT
> > Hello
>
[quoted text clipped - 41 lines]
> * For western locales, I've no idea what it does for locales where text
> is written right to left. Presumably it does what would be expected.
I would assume that locales only affect display order, not character
ordering within a string.
joes wrote:
> > String text = "aaa bbb aaa";
> > Pattern p = Pattern.compile("aaa");
> > Matcher m = p.matcher(text);
> > boolean b = m.matches();
b would be false here, because "aaa bbb aaa" does not match the
expression "aaa".
it *contains* a match for the expression, but is not a match itself.
aString.lastIndexOf(x) will find the index of the right most substring
of aString that is equal to x. Maybe thats what you want.
> > ... I now that
> > I can use "^" or "$" to specify that more precisely but
> > unfortunately can not change the expression (no access to the
> > source code).
If you have no access to the source code, what makes you think they
are using Pattern? Also, given that you have no guaranty about
matching the last, (hopefully matching the first is also good enough)
what would you do?