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>
we can also use
to match everything including newline without greedy
This will make the new line optional
It depends on the language, but there should be a modifier that you can add to the regex pattern. In PHP it is:
The s at the end causes the dot to match all characters including newlines.
"."
normally doesn't match line-breaks. Most regex engines allows you to add theS
-flag (also calledDOTALL
andSINGLELINE
) to make"."
also match newlines. If that fails, you could do something like[\S\s]
.Use RegexOptions.Singleline, it changes the meaning of . to include newlines
Regex.Replace(content, searchText, replaceText, RegexOptions.Singleline);
I had the same problem and solved it in probably not the best way but it works. I replaced all line breaks before I did my real match:
I am manipulating HTML so line breaks don't really matter to me in this case.
I tried all of the suggestions above with no luck, I am using .Net 3.5 FYI
I wanted to match a particular if block in java
If I use the regExp
it included the closing brace for the method block so I used
to exclude the closing brace from the wildcard match.