Reputation: 135
I have a string that can look like these examples:
lookbehind text bla lookahead
lookbehind text bla
text bla lookahead
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
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
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
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
Reputation: 784958
Following regex should work you:
(?:(?!lookbehind|lookahead)\b\w+\b| )+
Upvotes: 2