Regular expressions. Match a specific word between two words
I am using C #. I have a line:
wordA wordB wordC wordB wordD
I need to match all occurrences of word B between wordA and wordD. I am using lookahead and lookbehind to match all words wordA and worD like this:
(?<=wordA)(.*?)(?=wordD)
But something like
(?<=wordA)(wordB)(?=wordD)
nothing matches. What would be the best way to match all occurrences of wordB between wordA and wordD?
source to share
Place .*?
in images:
(?<=wordA.*?)wordB(?=.*?wordD)
See regex demo
Now the pattern means:
-
(?<=wordA.*?)
- (positive lookbehind) requires the presencewordA
, followed by any 0+ characters (as few as possible) immediately before ... -
wordB
- word B -
(?=.*?wordD)
- (positive) requires any 0+ characters (as few as possible) followedwordD
after them (so it can be immediately afterwordB
or after some characters).
If you need to account for multiline input, compile the regex with a flag RegexOptions.Singleline
so that it .
can match the newline character (or add a pattern with the inline modifier option (?s)
- (?s)(?<=wordA.*?)wordB(?=.*?wordD)
).
If the "words" are letters / numbers / underscores and you need to match them as whole words, remember to wrap wordA
, wordB
and wordD
with \b
(word boundaries).
Always check your regex against the target environment:
var s = "wordA wordB wordC wordB \n wordD";
var pattern = @"(?<=wordA.*?)wordB(?=.*?wordD)";
var result = Regex.Replace(s, pattern, "<<<$&>>>", RegexOptions.Singleline);
Console.WriteLine(result);
// => wordA <<<wordB>>> wordC <<<wordB>>>
// wordD
See C # demo .
source to share