Match 3 and 4 dividers and between; no less no more

I have a program command-linethat its first argument ( = argv[ 1 ] )is a regular expression pattern.

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

So I need a regular expression to check if the input I entered is correct from the user or not . This regular expression can be easily solved, but since I'm using C ++ library, std::regex_matchand this feature by default puts begin and end the assert ( ^and $) in the row, so quantifier nan-greedy ignored.

Let me clarify this question. If I want to compare /anything/, I can use /.*?/, but std::regex_matchconsider this as a template ^/.*?/$, and therefore, if the user enters: /anything/anything/anyhting/, std::regex_matchstill returns true when the input pattern is incorrect . std::regex_matchreturns only true or false , and the expected form of the template can only be text in accordance with the template. Since the picture is different, here I cannot provide you with all the possibilities, but I give you an example.
Should 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 /|@#
  • sthe letter at the beginning and g, iand 2 digits at the end are optional
  • std::regex_match true, - , false
  • +
  • *
  • g i
  • 3 /.// /./
  • ECMAScript 262


, , .
.

0
2

:

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

. regex101.com

, :

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

:

, :

  • ((?!\1).): , . , , . , , , .
  • (gi?|ig): , i, . , gg ii .
  • (\1\d\d?)?: ( g - . ) .
  • ( |i)?: g, i none: .
+1

, - :

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

:

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

- , ?:

+1

Source: https://habr.com/ru/post/1685295/


All Articles