user1451130
user1451130

Reputation: 135

regex with and without lookahead and lookbehind

I have a string that can look like these examples:

  1. lookbehind text bla lookahead
  2. lookbehind text bla
  3. text bla lookahead
  4. text bla

How do I match in every case text bla? text bla could be some string with whitespaces.

With regex (?<=lookbehind\s)[\w\s]+(?=\slookahead) it only works in case no. 1.

How to cover the other examples?

Upvotes: 1

Views: 174

Answers (4)

Francis Gagnon
Francis Gagnon

Reputation: 3675

As a different approach, you could use a regex replace to remove all unwanted text (the lookbehind and lookahead parts).

\s*(?:lookbehind|lookahead)\s*

Here is an example in C#:

string result = Regex.Replace(testInput, @"\s*(?:lookbehind|lookahead)\s*", "", RegexOptions.IgnoreCase);

Upvotes: 0

Bohemian
Bohemian

Reputation: 424983

It looks like you need the look-arounds to match certain text or start/end:

(?<=((lookbehind)|^)\s*)text bla(?=\s*(lookahead)|$)

Upvotes: 0

stema
stema

Reputation: 92976

Try this:

^(?:.*lookbehind\s*)?((?:(?!\slookahead)[\w ])+)

See it here on Regexr

This is a bit more complicated, I found no simpler solution. The result is always in capturing group 1, since I am matching "lookbehind\s*" if it is there.

^ Start of the string

(?:lookbehind\s*)? optional group, does match "lookbehind" with all following whitespaces.

Now there is the bit complicated part:

(                              # Start of the capturing group, stores the interesting part in group 1
    (?:                        # Non capturing group
        (?!\slookahead)[\w\s]  # Match [\w\s] only if it is not the start of the sequence "\slookahead"
    )+                         # repeat this
)

Upvotes: 5

anubhava
anubhava

Reputation: 784958

Following regex should work you:

(?:(?!lookbehind|lookahead)\b\w+\b| )+

Live Demo: http://www.rubular.com/r/rBB3GmgBec

Upvotes: 2

Related Questions