> 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.

Signature
Daniel Sjöblom
Remove _NOSPAM to reply by mail
Sharp - 02 May 2005 12:16 GMT
> > I would like to use the matches() method of the String class to test the
> > string's first 3 characters are identical.
[quoted text clipped - 11 lines]
> 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
hiwa - 02 May 2005 12:40 GMT
>>
>>
[quoted text clipped - 26 lines]
>Sharp
>
\\2 is \2 which means capturing group 2.
* means zero or more.
Read the javadoc for Pattern class.
Sharp - 02 May 2005 12:52 GMT
> >>>I would like to use the matches() method of the String class to test the
> >>>string's first 3 characters are identical.
[quoted text clipped - 24 lines]
> * means zero or more.
> Read the javadoc for Pattern class.
Thanks!
Cheers
Sharp
Christian Gudrian - 02 May 2005 12:43 GMT
Am Mon, 02 May 2005 11:16:57 GMT schrieb Sharp:
> Could you shed some light on the whole thing?
http://www.amk.ca/python/howto/regex/
Christian