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.

RegEx Woes! Please Help, Simple Question

Thread view: 
Saad Malik - 02 May 2005 01:51 GMT
Hi Guys/Gals

I got a quick question on regex. My input string consists of two
characters: [a-z][a-z].. However, I only want to match if the two
characters are different.

Example: ab,ck,dk,gk... are all valid, but aa,bb,cc,dd,ee... should not
match..

This is the regex I have so far, but it does not seem to work:

([a-z])(?!\1)[a-z]

complete program:
Pattern pat2 = Pattern.compile("([a-z])(?!\1)[a-z]");
Matcher m = pat2.matcher("aa");
System.out.println(m.matches()); THIS PRINTS TRUE but aa should not be
true.

Thanks in Advance,
Saad.
Alan Krueger - 02 May 2005 02:02 GMT
> ([a-z])(?!\1)[a-z]

Try two backslashes.  One backslash is interpreted by the Java compiler,
 so you'll need to quote it with another so it makes it to the regular
expression compiler.
Saad Malik - 02 May 2005 02:10 GMT
You are a genius! It worked.

Thanks,
Saad
Sharp - 02 May 2005 13:02 GMT
> > ([a-z])(?!\1)[a-z]
>
> Try two backslashes.  One backslash is interpreted by the Java compiler,
>   so you'll need to quote it with another so it makes it to the regular
> expression compiler.

What does the '?' mean?

Cheers
Sharp
Thomas Schodt - 02 May 2005 15:23 GMT
>>>([a-z])(?!\1)[a-z]

> What does the '?' mean?

Non-capturing groups are described just before backslashes
<http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html#bs>
John C. Bollinger - 02 May 2005 17:06 GMT
>>>([a-z])(?!\1)[a-z]
>>
[quoted text clipped - 3 lines]
>
> What does the '?' mean?

The "(?!" introduce a "zero-width negative lookahead assertion", which
is closed by a matching ")".  See the Pattern API docs for more on
Java's regex syntax, although it's fairly standard.  For details on the
_meaning_ of this sort of thing, Google for tutorials on regexes.

Signature

John Bollinger
jobollin@indiana.edu



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.