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>
In the context of use within languages, regular expressions act on strings, not lines. So you should be able to use the regex normally, assuming that the input string has multiple lines.
In this case, the given regex will match the entire string, since "<FooBar>" is present. Depending on the specifics of the regex implementation, the $1 value (obtained from the "(.*)") will either be "fghij" or "abcde\nfghij". As others have said, some implementations allow you to control whether the "." will match the newline, giving you the choice.
Line-based regular expression use is usually for command line things like egrep.
It depends on the language, but there should be a modifier that you can add to the regex pattern. In PHP it is:
/(.*)<FooBar>/s
The s at the end causes the dot to match all characters including newlines.
Try this:
((.|\n)*)<FooBar>
It basically says "any character or a newline" repeated zero or more times.
"."
normally doesn't match line-breaks. Most regex engines allows you to add the S
-flag (also called DOTALL
and SINGLELINE
) to make "."
also match newlines.
If that fails, you could do something like [\S\s]
.
Note that (.|\n)*
can be less efficient than (for example) [\s\S]*
(if your language's regexes support such escapes) and than finding how to specify the modifier that makes . also match newlines. Or you can go with POSIXy alternatives like [[:space:][:^space:]]*
.
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:
mystring= Regex.Replace(mystring, "\r\n", "")
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
Use RegexOptions.Singleline, it changes the meaning of . to include newlines
Regex.Replace(content, searchText, replaceText, RegexOptions.Singleline);