#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    [REGEX] How to make this regex expression more specific?


    [sS]?(\d{1,2})

    I would like to get a \d{2} if [sS] was found, else accept both
    \d{1} and \d{2}

    Thank you for your help.


    Regards,
    Zananok
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    You could use alternation:

    ([sS]\d{2})|([^sS]\d{1,2})

    I tried it in Perl, it seems to be doing what you want.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    And with just a little change should work in a posix or gnu ERE:
    Code:
    ([sS][0-9]{2})|([^sS][0-9]{1,2})
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    So, Spacebar, thank you for the comment, it seems to be saying that \d is not posix or gnu ERE, so that you replace it with a character class [0-9], I didn't know this was neither posix nor GNU.

    But it is a quite common feature of regex nowodays in many languages. And the OP used that syntax, so I would tend to think that this is available to her or him.

    Thank you for the information, anyway.

IMN logo majestic logo threadwatch logo seochat tools logo