Help with Regular Expressions in Java

Discussion in 'Java' started by sssrsl, May 1, 2007.

  1. 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
     
  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
    Try this pattern

    Code:
    p[. ]?o([. ]+)?(\d+)
    
     
  3. sssrsl

    sssrsl New Member

    Joined:
    May 1, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Perfect!! Thanks Pradeep!!

    Customized it a little to suit my requirements.

    Thanks again

    Sai
     
  4. sssrsl

    sssrsl New Member

    Joined:
    May 1, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Sorry :-( Forgot to add the pattern. Might be useful to others

    "p[. ]?o([. ]+)?(\\d+)|\\bpo\\s*box\\b"

    Thanks
    Sai
     

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