RegEx Help one more time

Discussion in 'Java' started by Ken Kast, Feb 24, 2007.

  1. Ken Kast

    Ken Kast Guest

    Here's my pattern:
    pattern =
    "\\b[A-Z]([A-Z0-9]|[-+_/&.](?=[A-Z0-9])|[(][A-Z0-9]([A-Z0-9]|[-+_/&.](?=[A-Z0-9]))*[)])+\\b";

    With the string AB(CDE), it finds only AB(CDE. Why isn't the closing parens
    found and why is it accepting the string without it?

    Thanks.

    Ken
     
    Ken Kast, Feb 24, 2007
    #1
    1. Advertising

  2. Ken Kast writes:

    > Here's my pattern:
    > pattern =
    > "\\b[A-Z]([A-Z0-9]|[-+_/&.](?=[A-Z0-9])|[(][A-Z0-9]([A-Z0-9]|[-+_/&.](?=[A-Z0-9]))*[)])+\\b";
    >
    > With the string AB(CDE), it finds only AB(CDE. Why isn't the closing
    > parens found and why is it accepting the string without it?


    I submit the following little program as an example of how you can
    study such problems yourself in a kind of experimental way. However,
    it doesn't find "AB(CDE", nor do I see how it could when the branch
    that matches the opening "(" ends with the closing ")".

    To match the closing paren before a word boundary there has to be a
    word character immediately after it.

    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    class Foo {
    public static void main(String [] _) {
    String pattern = "\\b[A-Z]("
    + "[A-Z0-9]"
    + "|[-+_/&.](?=[A-Z0-9])"
    + "|[(][A-Z0-9]([A-Z0-9]|[-+_/&.](?=[A-Z0-9]))*[)]"
    + ")+\\b";

    String text = "First AB(CDE) ends at a non-word-boundary, "
    + "second GH(IJ)K ends at a word-boundary."
    + "Third LM(NOP)q should be caught.";

    Matcher m = Pattern.compile(pattern).matcher(text);
    while (m.find()) {
    System.out.println(m.group());
    }
    }
    }
     
    Jussi Piitulainen, Feb 24, 2007
    #2
    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. Michael
    Replies:
    4
    Views:
    437
    Matt Hammond
    Jun 26, 2006
  2. Replies:
    3
    Views:
    794
    Reedick, Andrew
    Jul 1, 2008
  3. Merciadri Luca
    Replies:
    4
    Views:
    835
  4. Steven D'Aprano
    Replies:
    0
    Views:
    116
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    98
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page