Alex Rivera | Logout

How do I match any character across multiple lines in a regular expression?

Asked 2008-10-01T18:48:22.160
661

For example, this regex

(.*)<FooBar>

will match:

abcde<FooBar>

But how do I get it to match across multiple lines?

abcde
fghij<FooBar>
Edit
Report

2 Answers

58
([\s\S]*)<FooBar>

The dot matches all except newlines (\r\n). So use \s\S, which will match ALL characters.

answered 2012-07-19T17:59:45.447
9

"." 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].

answered 2008-10-01T18:52:28.233

Your Answer