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 c++ 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:
- delimiters are
/|@#
s
letter at the beginning andg
,i
and 2 digits at the end are optionalstd::regex_match
function returns true if the entire target character sequence can be match, otherwise return false- between first and second delimiter can be one-or-more
+
- between second and third delimiter can be zero-or-more
*
- between third and fourth can be g or i
- At least 3 delimiter should be match
/.//
not less so/./
should not be match - ECMAScript 262 is allowed for the pattern
NOTE
- May you would need to see may question about
std::regex_match
:
std::regex_match and lazy quantifier with strange behavior - I no need any C++ code, I just need a pattern.
- Do not try
d?([/|@#]).+?\1.*?\1[gi]?[gi]?\1?d?\d?\d?
. It fails. - My attempt so far:
^(?!s?([/|@#]).+?\1.*?\1.*?\1)s?([/|@#]).+?\2.*?\2[gi]?[gi]?\d?\d?$
- If you are willing to try, you should put
^
and$
around your pattern
If you need more details please comment me, and I will update the question.
Thanks.