For example, this regex
(.*)<FooBar>
will match:
abcde<FooBar>
But how do I get it to match across multiple lines?
abcde
fghij<FooBar>
For example, this regex
(.*)<FooBar>
will match:
abcde<FooBar>
But how do I get it to match across multiple lines?
abcde
fghij<FooBar>
Often we have to modify a substring with a few keywords spread across lines preceding the substring. Consider an xml element:
Suppose we want to modify the 81, to some other value, say 40. First identify
.UID.21..UID.
, then skip all characters including\n
till.PercentCompleted.
. The regular expression pattern and the replace specification are:The subgroup
(.|\n)
is probably the missing group$3
. If we make it non-capturing by(?:.|\n)
then the$3
is(<PercentComplete>)
. So the pattern andreplaceSpec
can also be:and the replacement works correctly as before.
([\s\S]*)<FooBar>
The dot matches all except newlines (\r\n). So use \s\S, which will match ALL characters.