File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Two Regular Expressions in one Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Two Regular Expressions in one" Watch "Two Regular Expressions in one" New topic

Two Regular Expressions in one

Peter Heide
Ranch Hand

Joined: Nov 04, 2006
Posts: 31
Regular Expressions can be used to validate if a String is in a valid format.

The following code checks if a string is valid using the following two rules

  • Starts with a letter from A to Z
  • Afterwards exactly 6 digits

  • Examples: A123456 is correct, A12345 is incorrect, AA is incorect

    The Java code is as follows:

    Result Output:

    ID A123456 is true
    ID A1 is false
    ID AA is false

    The problem is that a new rule comes into play:

    An ID is not valid when it starts with A33

    How can I code the first two rules and the new one together in one regular expression?

    Anubhav Anand
    Ranch Hand

    Joined: May 18, 2007
    Posts: 341

    Try this

    Visit this link. It has wonderful explanations.

    [Included the regex article url]
    [ April 03, 2008: Message edited by: Anubhav Anand ]
    Jim Yingst

    Joined: Jan 30, 2000
    Posts: 18671
    First a minor note - using lookingAt() is sort of an odd choice here, considering that you're also using \\z to force a match with the end of the pattern. It seems simpler to just use matches(), which requires a match of the entire expression, starting at the beginning and ending at, well, the end. Then you don't need to specify //z in the expression; it's implicit.

    Anyway, to add your new rule to the regex, you can use negative lookahead:

    Basically the (?!A33) means from this position (the beginning, in this case) the matcher needs to be able to scan ahead and not see A33. Then when it gets to the ), the pattern can forget about the lookahead expression and remember it's still at the beginning of the expression, and try to match the remainder of the pattern (the A-Z]{1}[0-9]{6}).

    "I'm not back." - Bill Harding, Twister
    Peter Heide
    Ranch Hand

    Joined: Nov 04, 2006
    Posts: 31
    You solved the problem and also your advice to improve my code worked. Thank you very much Anubhav and Jim!
    I agree. Here's the link:
    subject: Two Regular Expressions in one
    It's not a secret anymore!