Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > RegEx Woes! Please Help, Simple Question

Reply
Thread Tools

RegEx Woes! Please Help, Simple Question

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

 
Reply With Quote
 
 
 
 
Alan Krueger
Guest
Posts: n/a
 
      05-02-2005
Saad Malik wrote:
> ([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.
 
Reply With Quote
 
 
 
 
Saad Malik
Guest
Posts: n/a
 
      05-02-2005
You are a genius! It worked.

Thanks,
Saad

 
Reply With Quote
 
Sharp
Guest
Posts: n/a
 
      05-02-2005

> Saad Malik wrote:
> > ([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




 
Reply With Quote
 
Thomas Schodt
Guest
Posts: n/a
 
      05-02-2005
Sharp wrote:

>>>([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>
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      05-02-2005
Sharp wrote:

>>Saad Malik wrote:
>>
>>>([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?


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.


--
John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
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 make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
Simple Python REGEX Question johnny Python 4 05-12-2007 09:38 PM
a simple regex question John Salerno Python 6 04-02-2006 02:55 PM
RegEx Woes! Please Help, Simple Question frans abels Perl Misc 13 05-02-2005 07:51 AM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments