Reputation: 3160
How can I catch an arbitrary string between defined words using regular expressions in .Net, e.g. @”…DEFINED_WORD1 some_arbitrary_string DEFINED_WORD2…”? Unfortunately my experiments with “(?=)” and other patterns are unsuccessful :(
Upvotes: 1
Views: 281
Reputation: 384016
"A(.*?)Z"
This would capture strings between "A"
and "Z"
into group 1.
Upvotes: 2
Reputation: 38683
This will catch anything between the words, as long as there's a space after the first word and before the second. If there are multiple occurrences of WORD2
after WORD1
, the first one will be considered.
WORD1 (.*?) WORD2
This is the same, but doesn't require spaces (e.g. "WORD1, some string WORD2"
will match):
WORD1\b(.*?)\bWORD2
This will start from the first WORD1
and go on until the last WORD2
:
WORD1\b(.*)\bWORD2
Depending on the details of your case, this may be cleaner and easier without regular expressions.
Upvotes: 3