Java Regular Expression (java.util.regex ): Multiple Occurences, always guaranteed that it takes the

J

joes

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 would like to know if this is "guaranteed" or
somewehere specified that it takes "always" the "last" one. 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).

Does anybody knows more about that?

i.e.

String text = "aaa bbb aaa";
Pattern p = Pattern.compile("aaa");
Matcher m = p.matcher(text);
boolean b = m.matches();

thx
chees
Mark
 
I

Ian Wilson

joes said:
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
can not change the expression (no access to the source code).

Does anybody knows more about that?

i.e.

String text = "aaa bbb aaa";
Pattern p = Pattern.compile("aaa");
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.
 
D

Daniel Pitts

I think you are mistaken. It doesn't do that for me (see below).


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.



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.

I would assume that locales only affect display order, not character
ordering within a string.
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.

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top