5

This should be straightforward. I need a regular expression that selects everything that does not specifically contain a certain word.

So if I have this sentence: "There is a word in the middle of this sentence." And the regular expression gets everything but "middle", I should select everything in that sentence but "middle".

Is there any easy way to do this?

Thanks.

Matt
  • 23,363
  • 39
  • 111
  • 152

2 Answers2

2

It is not possible for a single regex match operation to be discontinuous.

You could use two capturing groups:

(.*)middle(.*)

Then concatenate the contents of capturing groups 1 and 2 after the match.

You may wish to enable the "dot also matches newline" option in your parser.
See for example Java's DOTALL, .NET's Singleline, Perl's s, etc.

Mike Clark
  • 10,027
  • 3
  • 40
  • 54
1

Positive lookaround is the way to go:

/^(.+)(?=middle)/ -- gets everything before middle, not including middle

and

/(?!middle)(.+)$/ -- gets everything after middle, not including middle

Then you just merge the results of both

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
  • When/if you do this make sure you enable the `.` to match newline characters as well. – Keng Dec 02 '10 at 18:29