String matches() method

Discussion in 'Java' started by Sharp, May 2, 2005.

  1. Sharp

    Sharp Guest

    Hi

    I have a string, for example:

    String str = "aaab";

    I would like to use the matches() method of the String class to test the
    string's first 3 characters are identical.
    I would like to do this in a general way by using regular expressions.

    Tried (but doesn't work):

    if (str.matches("xxx[a-zA-Z]"))
    return true;

    According to the regular expression, x is suppose to be a character.
    Not sure, why this is not working, or if it's even possible to do it using
    the matches method.
    Any help appreciated.

    Cheers
    Sharp
     
    Sharp, May 2, 2005
    #1
    1. Advertising

  2. Sharp wrote:
    > I would like to use the matches() method of the String class to test the
    > string's first 3 characters are identical.


    > Tried (but doesn't work):
    >
    > if (str.matches("xxx[a-zA-Z]"))
    > return true;
    >
    > According to the regular expression, x is suppose to be a character.


    Here, x is a character, that is 'x'. To achieve what you want you would
    need something like "((.)\\2\\2).*". But using regexes is not the right
    answer to this problem. It is better to write a method that just checks
    that the first three characters are the same. It's just a couple of
    lines of code, probably faster than using a regex too.

    --
    Daniel Sjöblom
    Remove _NOSPAM to reply by mail
     
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, May 2, 2005
    #2
    1. Advertising

  3. Sharp

    Sharp Guest


    > Sharp wrote:
    > > I would like to use the matches() method of the String class to test the
    > > string's first 3 characters are identical.

    >
    > > Tried (but doesn't work):
    > >
    > > if (str.matches("xxx[a-zA-Z]"))
    > > return true;
    > >
    > > According to the regular expression, x is suppose to be a character.

    >
    > Here, x is a character, that is 'x'. To achieve what you want you would
    > need something like "((.)\\2\\2).*". But using regexes is not the right
    > answer to this problem. It is better to write a method that just checks
    > that the first three characters are the same. It's just a couple of
    > lines of code, probably faster than using a regex too.


    Thanks for your help.
    Your suggested regular expression works, but I dont fully understand it.
    I assume that '\\2' means same as the previous character, which is '.'
    Not sure what the '*' means.
    Could you shed some light on the whole thing?

    Cheers
    Sharp
     
    Sharp, May 2, 2005
    #3
  4. Sharp

    hiwa Guest

    Sharp wrote:

    >>Sharp wrote:
    >>
    >>
    >>>I would like to use the matches() method of the String class to test the
    >>>string's first 3 characters are identical.
    >>>
    >>>
    >>>Tried (but doesn't work):
    >>>
    >>>if (str.matches("xxx[a-zA-Z]"))
    >>> return true;
    >>>
    >>>According to the regular expression, x is suppose to be a character.
    >>>
    >>>

    >>Here, x is a character, that is 'x'. To achieve what you want you would
    >>need something like "((.)\\2\\2).*". But using regexes is not the right
    >>answer to this problem. It is better to write a method that just checks
    >>that the first three characters are the same. It's just a couple of
    >>lines of code, probably faster than using a regex too.
    >>
    >>

    >
    >Thanks for your help.
    >Your suggested regular expression works, but I dont fully understand it.
    >I assume that '\\2' means same as the previous character, which is '.'
    >Not sure what the '*' means.
    >Could you shed some light on the whole thing?
    >
    >Cheers
    >Sharp
    >
    >

    \\2 is \2 which means capturing group 2.
    * means zero or more.
    Read the javadoc for Pattern class.
     
    hiwa, May 2, 2005
    #4
  5. Christian Gudrian, May 2, 2005
    #5
  6. Sharp

    Sharp Guest

    "hiwa" <> wrote in message
    news:d553k2$8k2$...
    > Sharp wrote:
    >
    > >>Sharp wrote:
    > >>
    > >>
    > >>>I would like to use the matches() method of the String class to test

    the
    > >>>string's first 3 characters are identical.
    > >>>
    > >>>
    > >>>Tried (but doesn't work):
    > >>>
    > >>>if (str.matches("xxx[a-zA-Z]"))
    > >>> return true;
    > >>>
    > >>>According to the regular expression, x is suppose to be a character.
    > >>>
    > >>>
    > >>Here, x is a character, that is 'x'. To achieve what you want you would
    > >>need something like "((.)\\2\\2).*". But using regexes is not the right
    > >>answer to this problem. It is better to write a method that just checks
    > >>that the first three characters are the same. It's just a couple of
    > >>lines of code, probably faster than using a regex too.
    > >>
    > >>

    > >
    > >Thanks for your help.
    > >Your suggested regular expression works, but I dont fully understand it.
    > >I assume that '\\2' means same as the previous character, which is '.'
    > >Not sure what the '*' means.
    > >Could you shed some light on the whole thing?
    > >
    > >Cheers
    > >Sharp
    > >
    > >

    > \\2 is \2 which means capturing group 2.
    > * means zero or more.
    > Read the javadoc for Pattern class.


    Thanks!

    Cheers
    Sharp
     
    Sharp, May 2, 2005
    #6
    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. rocalp
    Replies:
    0
    Views:
    382
    rocalp
    Feb 18, 2004
  2. John Salerno

    reusing parts of a string in RE matches?

    John Salerno, May 10, 2006, in forum: Python
    Replies:
    26
    Views:
    649
    Mirco Wahab
    May 13, 2006
  3. Aaron Scott
    Replies:
    7
    Views:
    379
    alex23
    Dec 2, 2008
  4. laredotornado

    Confusion about String.matches method

    laredotornado, May 31, 2011, in forum: Java
    Replies:
    20
    Views:
    827
    Esmond Pitt
    Jun 9, 2011
  5. Jochen Brenzlinger
    Replies:
    5
    Views:
    701
    Daniel Pitts
    Nov 21, 2011
Loading...

Share This Page