SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Threaded View

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Regular Expressions Help

    I am trying to create a regular expression to help me validate a particular string that users will be passing into my application. I thought I had it figured out until I found a few test cases that my expression gave a false positive for. Here are the details:

    1. The first six characters will be letters or spaces.
    2. The first character must be a letter.
    3. If the first six characters are not all letters, then the rest of the six are padded with spaces.

    Here are some examples of valid strings:

    Good:
    Code:
    ABCDEF0123
    GHIJK 0123
    LMNO  0123
    PQR   0123
    ST    0123
    U     0123
    So I quickly whipped up the below expression to validate these strings:

    Code:
    ([a-zA-Z]){1}([a-zA-Z]|\s){5}\d{4}
    The problem is this can create false positives because I am not properly meeting requirement number 3. Here are some strings that cause false positives:

    Bad:
    Code:
    ABCD F0123
    GHI JL0123
    MN PQR0123
    S UVWX0123
    I'm not the best at regular expressions so I am kind of stumped as to how to proceed. How do I check the first six characters so that they are letters until a space is found and then it is padded until it reaches 6 characters. Thank you in advance for your help.
    Last edited by viper2843; Feb 22, 2010 at 22:13. Reason: messed up the regex.


Bookmarks

Posting Permissions

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