Help in Regular expression using java

Discussion in 'Java' started by vithasekar, Apr 2, 2007.

  1. vithasekar

    vithasekar New Member

    Joined:
    Apr 2, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I want to do pattern matching using java.

    My string is (a<b)

    I tried with the following patterns.
    [(][a][<][)]$

    and

    [(][a][/<][)]$

    but these patterns doesn't match.
    can any one tell me how to match operators like < , >, <=, >=, + ..


    Thank u..
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
  3. vithasekar

    vithasekar New Member

    Joined:
    Apr 2, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    if(String.matches("\([a]<\")

    I am getting error: unrecognized escape sequence.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You have not copied the things correctly. You are missing the end bracket.
     
  5. vithasekar

    vithasekar New Member

    Joined:
    Apr 2, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    After that also, I am getting the same error

    if(strng.matches("\([a]<\)")
     
  6. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Here's an example of using Regular Expressions in Java

    Code:
    // Compile regular expression
        String patternStr = "b";
        Pattern pattern = Pattern.compile(patternStr);
        
        // Determine if there is an exact match
        CharSequence inputStr = "a b c";
        Matcher matcher = pattern.matcher(inputStr);
        boolean matchFound = matcher.matches(); // false
        
        // Try a different input
        matcher.reset("b");
        matchFound = matcher.matches();         // true
        
        // Determine if pattern matches beginning of input
        matchFound = matcher.lookingAt();       // false
    
     
  7. vithasekar

    vithasekar New Member

    Joined:
    Apr 2, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Thank u sir. It's working.
     
  8. sssrsl

    sssrsl New Member

    Joined:
    May 1, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi

    I need to check for a PO Box addresses in a string. I should not allow a pobox in a address Line. For that I need a pattern and willl match this pattern with my string

    Ex:***** something like this ******
    Pattern poBoxPattern = Pattern.compile("\\bp\\s*\\d") ....
    Matcher matcher = poBoxPattern.matcher(locationAddressStreet);
    if(matcher.find()){

    }


    So, could anyone help me with making the pattern.
    My pattern should recognize sub strings like...
    1. po 123 ( meaning: po followed by any integer/integers )
    2. p o 123
    3. p.o. 123
    4. p. o. 123
    5. po box
    6. po box
    7. p o box
    8. p o 123

    Note: "po sometext" or "p o sometext" should not be recognized.

    Any help is truly appreciated.

    Thanks
    Sai
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    sssrsl, have a separate thread for the query as that will give more visibility to experts as well as help others to search.
     
  10. sssrsl

    sssrsl New Member

    Joined:
    May 1, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    I did. Thanks, for the suggestion.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice