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.

2
  • eh, just copy the data to a string var and do a search/replace....probably cheaper than RE anyway. Commented Dec 2, 2010 at 18:36
  • Can this word occur more than once in the sentence? Commented Dec 2, 2010 at 19:50

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

When/if you do this make sure you enable the . to match newline characters as well.
That does not guarantee that neither capture group contains "middle". Should it?
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

1 Comment

When/if you do this make sure you enable the . to match newline characters as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.