String matches() method

S

Sharp

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
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Sharp said:
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.
 
S

Sharp

Sharp said:
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
 
H

hiwa

Sharp said:
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.
 
S

Sharp

hiwa said:
Sharp said:
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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top