RegEx Woes! Please Help, Simple Question

Discussion in 'Java' started by Saad Malik, May 2, 2005.

  1. Saad Malik

    Saad Malik Guest

    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.
     
    Saad Malik, May 2, 2005
    #1
    1. Advertising

  2. Saad Malik

    Alan Krueger Guest

    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.
     
    Alan Krueger, May 2, 2005
    #2
    1. Advertising

  3. Saad Malik

    Saad Malik Guest

    You are a genius! It worked.

    Thanks,
    Saad
     
    Saad Malik, May 2, 2005
    #3
  4. Saad Malik

    Sharp Guest


    > 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
     
    Sharp, May 2, 2005
    #4
  5. 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>
     
    Thomas Schodt, May 2, 2005
    #5
  6. 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
     
    John C. Bollinger, May 2, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KK
    Replies:
    2
    Views:
    664
    Big Brian
    Oct 14, 2003
  2. Red

    IE7 Woes - help please!

    Red, Mar 5, 2007, in forum: HTML
    Replies:
    5
    Views:
    348
    Jukka K. Korpela
    Mar 6, 2007
  3. Replies:
    3
    Views:
    808
    Reedick, Andrew
    Jul 1, 2008
  4. Karl

    VB.NET/ASP.NET Web service woes! Please help!! BMUK

    Karl, Feb 26, 2005, in forum: ASP .Net Web Services
    Replies:
    2
    Views:
    138
  5. frans abels

    RegEx Woes! Please Help, Simple Question

    frans abels, May 1, 2005, in forum: Perl Misc
    Replies:
    13
    Views:
    201
    Joonas Timo Taavetti Kekoni
    May 2, 2005
Loading...

Share This Page