Results 1 to 2 of 2
  1. #1
    romina is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

    Default I can't find the right regular expression

    Hello, I want a given string to match a certain pattern and I am using the following function
    Java Code:
    public boolean matches(String regex) from java.lang.String
    The constraints the strings have to satisfy are the following
    • At least 2 letter a-zA-z
    • Zero or more times an apostrophe (')
    • Zero or more times a space

    for example James o'Hara is valid string.

    The problem is I can't find the right pattern/regular expression.

    I tried this: string.matches([a-zA-Z]{2,}[ ']*) but the example didn't work.
    Can anyone help me? I am using eclipse and Java5.0
    Thanks

  2. #2
    barney is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    The regex I used was
    Java Code:
    [a-zA-Z]{2,}[a-zA-Z ']*
    
    It seemed to work:
    Ouch this totally 'sux0rz - false (numbers)
    Ouch this totally 'sux.rz - false (improper character)
    Ouch this totally 'suxorz - true
    a - false (not enough beginning letters)
    a' - false (not enough beginning letters)
    a ' - false (not enough beginning letters)
    a b' - false (not enough beginning letters)
    a'b - false (not enough beginning letters)
    ab' - true
    ab ' - true
    The reason that you were having problems is because you weren't allowing for letters after a space or after an apostrophe. The regex string you created assumes that there is no input other than a space or an apostrophe after a space or an apostrophe.
    Greetings.

Similar Threads

  1. regular expression for unicode
    By tharhan in forum Advanced Java
    Replies: 0
    Last Post: 04-01-2008, 11:53 PM
  2. Regular expression for file path
    By ravian in forum New To Java
    Replies: 3
    Last Post: 01-25-2008, 09:24 PM
  3. Regular expression with Unions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-09-2008, 01:03 PM
  4. Regular expression with Intersections
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-09-2008, 01:03 PM
  5. Boolean Expression
    By ritwik07 in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 05:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •