Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > String matches() method

Reply
Thread Tools

String matches() method

 
 
Sharp
Guest
Posts: n/a
 
      05-02-2005
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





 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Daniel_Sj=F6blom?=
Guest
Posts: n/a
 
      05-02-2005
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
 
Reply With Quote
 
 
 
 
Sharp
Guest
Posts: n/a
 
      05-02-2005

> 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






 
Reply With Quote
 
hiwa
Guest
Posts: n/a
 
      05-02-2005
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.
 
Reply With Quote
 
Christian Gudrian
Guest
Posts: n/a
 
      05-02-2005
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
 
Reply With Quote
 
Sharp
Guest
Posts: n/a
 
      05-02-2005

"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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to convert string function to string method? Dr. Phillip M. Feldman Python 8 12-08-2009 09:42 AM
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
Simple error : method format(String, Object[]) is not applicable for the arguments (String, String) ankur Java 1 08-27-2007 06:31 AM
Is there a getEnv(String string) method in javax.servlet.http package? RC Java 1 07-07-2005 03:49 PM
Why the method of concat(String str) in String Class doesn't work? Bruce Sam Java 7 11-09-2004 02:17 PM



Advertisments