João Paulo
João Paulo

Reputation: 6670

Match string not containing substring in the middle

The question is about matching a delimited substring that does not have an unescaped dot.

I have words separated by dots. An escaped dot isn't a separator.

An example pattern for this situation using wild cards would be asd.*.xyz. Look the examples:

I want to match this (with the . escaped):

asd.qwe\.ert.xyz

or

asd.qwe\ert.xyz

But I don't want to match this (when the . is not escaped):

asd.qwe.ert.xyz

The slash not followed by a dot is just a char.

I tried using negative lookahead:

^asd\.(?:(?![^\\]\.).)*\.xyz$

But in this case, the last escaped dot is not allowing it to work. How can I achieve this?

Upvotes: 1

Views: 54

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626690

You need to match any text, and if there is any dot, you want that dot to be preceded with a backslash. You may use

^asd\.[^.\\]*(?:\\.[^.\\]*)*\.xyz$

See the regex demo

  • ^asd\. - start of string and then asd.
  • [^.\\]*(?:\\.[^.\\]*)* - 0 or more chars other than . and \, and then 0 or more repetitions of any escaped sequence (\\.) and then 0 or more chars other than . and \
  • \.xyz$ - .xyz at the end of string.

Another way of writing the pattern is ^asd\.(?:\\.|[^.\\])*\.xyz$ (see demo), but it is a bit less efficient.

Upvotes: 1

Related Questions