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

Discussion in 'Java' started by joes, May 25, 2007.

  1. joes

    joes Guest

    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
     
    joes, May 25, 2007
    #1
    1. Advertising

  2. joes

    Ian Wilson Guest

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

    joes wrote:
    > 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.
     
    Ian Wilson, May 25, 2007
    #2
    1. Advertising

  3. joes

    Daniel Pitts Guest

    On May 25, 2:19 am, Ian Wilson <> wrote:
    > joes wrote:
    > > 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.


    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?
     
    Daniel Pitts, May 25, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,336
  2. Tom Maki
    Replies:
    4
    Views:
    582
    Tom Maki
    Oct 26, 2004
  3. Replies:
    4
    Views:
    1,539
  4. Mantorok Redgormor

    EXIT_SUCCESS guaranteed to always be zero?

    Mantorok Redgormor, Nov 25, 2003, in forum: C Programming
    Replies:
    25
    Views:
    770
    Grumble
    Nov 27, 2003
  5. Des Small
    Replies:
    8
    Views:
    282
    Hallvard B Furuseth
    Aug 24, 2004
Loading...

Share This Page