Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

For example, this regex

(.*)<FooBar> 

will match:

abcde<FooBar> 

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

abcde fghij<FooBar> 
like image 697
andyuk Avatar asked Oct 01 '08 18:10

andyuk


People also ask

How do you match a character sequence in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What is the regex mode modifier for multiline?

The "m" modifier specifies a multiline match. It only affects the behavior of start ^ and end $. ^ specifies a match at the start of a string.

How do you match a character including newline in regex?

By default in most regex engines, . doesn't match newline characters, so the matching stops at the end of each logical line. If you want . to match really everything, including newlines, you need to enable "dot-matches-all" mode in your regex engine of choice (for example, add re. DOTALL flag in Python, or /s in PCRE.

What regex matches any character?

Matching a Single Character Using Regex By default, the '. ' dot character in a regular expression matches a single character without regard to what character it is. The matched character can be an alphabet, a number or, any special character.


Video Answer


1 Answers

Try this:

((.|\n)*)<FooBar> 

It basically says "any character or a newline" repeated zero or more times.

like image 146
levik Avatar answered Sep 21 '22 11:09

levik