Fixed regex prefix

I have a string of regular expressions and I want to classify whether this regular expression has a fixed prefix or not.

For instance:

abcdef.*g[0abc]{0,5}hi has the prefix abcdef

]1234vac.*12345 has the prefix ]1234vac

(abc)+123 has the prefix abc

but

[Az]+12345 does not have a fixed prefix (it starts with an unknown number of characters from the set of Az)

Do I really understand that this problem will not be solved in a general way?

+5
source share
1 answer

Try this RegEx:

 ^( ( # GENERAL before . (Dot) (?!\w+\?) # DO NOT MATCH if contains ? [\w\]\)]+ # Word, ] or ) characters 1 or more times )| (?:\((\w+)\))| # Words in between BRACKETS () ( # BEFORE . (Dot) with ?, * or + [\w\]\)]+ # Select Characters (?![?*+]) # DO NOT select last character if there is ?, * or + after it ) ) 

Live Demo on Regex101

Tell me about other examples that do not work, and I will change that. However, I tested all the examples in your question and comments

In addition, how can you even solve this difficult question! ;)

+1
source

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


All Articles