Regular expressions - pattern matching problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • basm101
    New Member
    • Sep 2007
    • 12

    #1

    Regular expressions - pattern matching problem

    Hi,

    I am trying to match a string like this: test (AB12 A)
    including the brackets.

    So far, I have this code:
    Code:
    Pattern p = Pattern.compile("[A-Z]+/s+\\([A-Z]{2}[0-9]{2}/s+[A-Z]{1}\\)");
    Matcher m = p.matcher(string to match in here);
    boolean b = m.matches();
    if(b){
      System.out.println("match seen");
    }else{
    System.out.println("Not a match");
    }
    As you may have guessed...it doesn't work. What am I doing wrong ? Am I right with the two backslashes to match a bracket and with the /s to match a blank space ?

    thanks !
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by basm101
    Hi,

    I am trying to match a string like this: test (AB12 A)
    including the brackets.

    So far, I have this code:
    Code:
    Pattern p = Pattern.compile("[A-Z]+/s+\\([A-Z]{2}[0-9]{2}/s+[A-Z]{1}\\)");
    Matcher m = p.matcher(string to match in here);
    boolean b = m.matches();
    if(b){
      System.out.println("match seen");
    }else{
    System.out.println("Not a match");
    }
    As you may have guessed...it doesn't work. What am I doing wrong ? Am I right with the two backslashes to match a bracket and with the /s to match a blank space ?

    thanks !
    Do you want to match that particular string only or do you want to match all strings with that particular structure?

    Comment

    • basm101
      New Member
      • Sep 2007
      • 12

      #3
      Originally posted by r035198x
      Do you want to match that particular string only or do you want to match all strings with that particular structure?
      All strings with that structure. Sorry if I didnt make myself clear

      thanks

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by basm101
        All strings with that structure. Sorry if I didnt make myself clear

        thanks
        And by "that structure" you mean any two capital letters followed by two digits followed by any one capital letter and all enclosed in brackets?

        Comment

        • basm101
          New Member
          • Sep 2007
          • 12

          #5
          Originally posted by r035198x
          And by "that structure" you mean any two capital letters followed by two digits followed by any one capital letter and all enclosed in brackets?
          yes that is exactly what I mean

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by basm101
            yes that is exactly what I mean
            I see now that you are also trying to match a space so there should be a space before the last character?
            In that case try
            Code:
            ^\\([A-Z]{2}\\d{2}\\s[A-Z]{1}\\)$

            Comment

            Working...