1

I'm currently writing a regular expression to search in files using visual studio regex pattern, the pattern im working on is something like this

  1. starts with string 1
  2. doesnt contain string2 in between strings 1 and 3
  3. ends with string 3

Im modifying a previous regex that i used sometime ago (2 years?) but can't come up with something that fits the need. This is currently im trying to work on.

\bword1\W+(?:\w+\W+)*^((?!word2).)*$word3\b

Can anyone teach me a thing or two about regex?

Thank you

parsley72
  • 8,449
  • 8
  • 65
  • 98
user1465073
  • 315
  • 8
  • 22

1 Answers1

2

Look:

  • starts with string 1 - string1
  • doesn't contain string2 in between strings 1 and 3 - here, you need to use a . tempered with a negative lookahead - (?:(?!string1|string2).)*? (note that to match across line, in Visual Studio S&R you need to use [\s\S\r] instead of a . (\r is necessary here for the reason [\s\S] does not match a line break in VS S&R regex))
  • ends with string 3 - string3.

So, the whole expression is

string1(?:(?!string1|string2).)*?string3

See the regex demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563