Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

String matches() method

Thread view: 
Sharp - 02 May 2005 09:15 GMT
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
Daniel Sjöblom - 02 May 2005 11:12 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.