1

I need to find all combinations of two words, say from and to, not having a particular word between them, say exclude

from
exclude
to

from
include
to

Only the second from -> to should match. Currently I can do this but just excluding single characters

from([^x]*?)to

Michele
  • 8,563
  • 6
  • 45
  • 72
  • 1
    you probably want a [lookaround](http://www.regular-expressions.info/lookaround.html) – Marc B Jul 13 '16 at 16:33
  • How strictly should the words be matched? Would `excluded` be OK or not? What about `fromage` or `today` - should they count as start/end markers, too? – Tim Pietzcker Jul 13 '16 at 16:46
  • Tim I'm looking for exact words match. So they won't be what I'm looking for – Michele Jul 13 '16 at 17:54

1 Answers1

1

You need to use a tempered greedy token:

When to Use this Technique
Suppose our boss now tells us that we still want to match up to and including {END}, but that we also need to avoid stepping over a {MID} section, if it exists. Starting with the lazy dot-star version to ensure we match up to the {END} delimiter, we can then temper the dot to ensure it doesn't roll over {MID}:

{START}(?:(?!{MID}).)*?{END}

If more phrases must be avoided, we just add them to our tempered dot:

 {START}(?:(?!{MID})(?!{RESTART}).)*?{END}

This is a useful technique to know about.

Use

from((?:(?!exclude|to|from).)*)to

Use DOTALL modifier if you need the dot to match newlines.

See the regex demo

See a related SO thread about a tempered greedy token.

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