Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
joes - 25 May 2007 10:01 GMT
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
Ian Wilson - 25 May 2007 10:19 GMT
> 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?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.