Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expressions fixed prefix

Tags:

regex

I have the regex string and I want to classify, is this regex has fixed prefix or no.

For example:

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

]1234vac.*12345 has prefix ]1234vac

(abc)+123 has prefix abc

but

[A-z]+12345 doesn't has fixed prefix (it starts from unknown number of symbols from set A-z)

Am I truly understand that this problem will not be solved in a general form?

like image 523
dgabriel Avatar asked Nov 09 '22 17:11

dgabriel


1 Answers

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 any other examples that do not work and I'll change this. I have however tested on all the examples in your question, and comments

Also, how is it even possible to come up with a question this complicated! ;)

like image 165
Kaspar Lee Avatar answered Nov 15 '22 07:11

Kaspar Lee