0

I have a command-line program that its first argument ( = argv[ 1 ] ) is a regex pattern.

./program 's/one-or-more/anything/gi/digit-digit'

So I need a regex to check if the entered input from user is correct or not. This regex can be solve easily but since I use library and std::regex_match and this function by default puts begin and end assertion (^ and $) at the given string, so the nan-greedy quantifier is ignored.

Let me clarify the subject. If I want to match /anything/ then I can use /.*?/ but std::regex_match considers this pattern as ^/.*?/$ and therefore if the user enters: /anything/anything/anyhting/ the std::regex_match still returns true whereas the input-pattern is not correct. The std::regex_match only returns true or false and the expected pattern form the user can only be a text according to the pattern. Since the pattern is various, here, I can not provide you all possibilities, but I give you some example.
Should be match

/.//
s/.//
/.//g
/.//i
/././gi
/one-or-more/anything/
/one-or-more/anything/g/3
/one-or-more/anything/i
/one-or-more/anything/gi/99
s/one-or-more/anything/g/4
s/one-or-more/anything/i
s/one-or-more/anything/gi/54

and anything look like this pattern

Rules:

  1. delimiters are /|@#
  2. s letter at the beginning and g, i and 2 digits at the end are optional
  3. std::regex_match function returns true if the entire target character sequence can be match, otherwise return false
  4. between first and second delimiter can be one-or-more +
  5. between second and third delimiter can be zero-or-more *
  6. between third and fourth can be g or i
  7. At least 3 delimiter should be match /.// not less so /./ should not be match
  8. ECMAScript 262 is allowed for the pattern

NOTE


If you need more details please comment me, and I will update the question.
Thanks.

11
  • I suspect I am missing something here, and I haven't allowed for different delimiters yet - but would this not work? regex101.com/r/Zb6DXL/1
    – Theo
    Feb 25, 2017 at 19:44
  • @Theo Please wait that I can test it. It seem look like correct Feb 25, 2017 at 19:47
  • @Theo NO your pattern matches one or more digit at the end. test it with .../gi/33333333333 and so on Feb 25, 2017 at 19:51
  • ok so the \d+ will be to blame we can swap this for \d{1,2} to only match one or two - see updated version: regex101.com/r/Zb6DXL/2 - If this works I will provide answer and explain the regex a bit
    – Theo
    Feb 25, 2017 at 19:53
  • 1
    Can you update your list of what should match?
    – Theo
    Feb 25, 2017 at 20:06

2 Answers 2

1

You could use this regular expression:

^s?([/|@#])((?!\1).)+\1((?!\1).)*\1((gi?|ig)(\1\d\d?)?|i)?$

See regex101.com

Note how this also rejects these cases:

///anything/
/./anything/gg
/./anything/ii
/./anything/i/12

How it works:

Some explanation of the parts that are different:

  • ((?!\1).): this will match any character that is not the delimiter. This way you are sure you can keep track of the exact number of delimiters used. You can this way also prevent that the first character after the first delimiter, is again that delimiter, which should not be allowed.
  • (gi?|ig): matches any of the valid modifier combinations, except a sole i, which is treated separately. So this also excludes gg and ii as valid character sequences.
  • (\1\d\d?)?: optionally allows for an extra delimiter (after a g modifier -- see previous) to be added with one or two digits following it.
  • ( |i)?: for the case there is no g modifier present, but just the i or none: then no digits are allowed to follow.
21
  • Many thanks but it should not match s/.///33 at least g is needed link Feb 25, 2017 at 20:23
  • 1
    I had already updated my regex and answer to deal with that. Please check again.
    – trincot
    Feb 25, 2017 at 20:27
  • Okay I did a fast test and it worked. Please add more detail that how it works and I after full test it with my program accept your answer Feb 25, 2017 at 20:33
  • 1
    I don't really have a book to suggest: I just learned by doing.
    – trincot
    Feb 25, 2017 at 21:45
  • 1
    @trincot ok fair enough, I was busy trying to make it support that exact scenario, I will delete my answer :(
    – Theo
    Feb 25, 2017 at 21:56
1

This is a tricky one, but I took the challenge - here is what I have ended up with:

^s?([\/|@#])(?:(?!\1).)+\1(?:(?!\1).)*\1(?:i|(?:gi?|ig)(\1\d{1,2})?)?$

Pattern breakdown:

  • ^ matches start of string
  • s? matches an optional 's' character
  • ([\/|@#]) matches the delimeter characters and captures as group 1
  • (?:(?!\1).)+ matches anything other than the delimiter character one or more times (uses negative lookahead to make sure that the character isn't the delimiter matched in group 1)
  • \1 matches the delimiter character captured in group 1
  • (?:(?!\1).)* matches anything other than the delimiter character zero or more times
  • \1 matches the delimiter character captured in group 1
  • (?: starts a new group
    • i matches the i character
    • | or
    • (?:gi?|ig) matches either g, gi, or ig
    • (\1\d{1,2})? followed by an optional extra delimiter and 0-9 once or twice
  • )? closes group and makes it optional
  • $ matches end of string

I have used non capturing groups throughout - these are groups that start ?:

6
  • We are here to learn not just downvote or upvote. Since I can learn a little thing from you answer it is worth it to me Feb 25, 2017 at 22:01
  • OK - I've added a disclaimer just to be clear it doesn't cope with your current scenario; but glad if it helps you learn.
    – Theo
    Feb 25, 2017 at 22:06
  • The answer trincot is correct as I wanted. However thanks for your attempt. Just to mention, it should not match s/./g/i/33 but your pattern matches it. The digits are allowed if there is a g flag. Feb 25, 2017 at 22:12
  • Could not bear to leave an incorrect answer so have updated
    – Theo
    Feb 25, 2017 at 22:17
  • Okay looks good. You do your best. Just remove /m at the end of your pattern Feb 25, 2017 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.