Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match any character including new lines

Tags:

regex

perl

Is there a regex to match "all characters including newlines"?

For example, in the regex below, there is no output from $2 because (.+?) doesn't include new lines when matching.

$string = "START Curabitur mollis, dolor ut rutrum consequat, arcu nisl ultrices diam, adipiscing aliquam ipsum metus id velit. Aenean vestibulum gravida felis, quis bibendum nisl euismod ut. 

Nunc at orci sed quam pharetra congue. Nulla a justo vitae diam eleifend dictum. Maecenas egestas ipsum elementum dui sollicitudin tempus. Donec bibendum cursus nisi, vitae convallis ante ornare a. Curabitur libero lorem, semper sit amet cursus at, cursus id purus. Cras varius metus eu diam vulputate vel elementum mauris tempor. 

Morbi tristique interdum libero, eu pulvinar elit fringilla vel. Curabitur fringilla bibendum urna, ullamcorper placerat quam fermentum id. Nunc aliquam, nunc sit amet bibendum lacinia, magna massa auctor enim, nec dictum sapien eros in arcu. 

Pellentesque viverra ullamcorper lectus, a facilisis ipsum tempus et. Nulla mi enim, interdum at imperdiet eget, bibendum nec END";

$string =~ /(START)(.+?)(END)/;

print $2;
like image 257
kurotsuki Avatar asked Nov 28 '11 22:11

kurotsuki


People also ask

How do you match a character including newline in RegEx?

We want any number of characters that are not double quotes or newlines between the quotes. So the proper regex is "[^"\r\n]*". If your flavor supports the shorthand \v to match any line break character, then "[^"\v]*" is an even better solution.

What does \b mean in RegEx?

The metacharacter \b is an anchor like the caret and the dollar sign. It matches at a position that is called a “word boundary”. This match is zero-length. There are three different positions that qualify as word boundaries: Before the first character in the string, if the first character is a word character.

What is multiline in RegEx?

Multiline option, or the m inline option, enables the regular expression engine to handle an input string that consists of multiple lines. It changes the interpretation of the ^ and $ language elements so that they match the beginning and end of a line, instead of the beginning and end of the input string.

Does \s match new line?

According to regex101.com \s : Matches any space, tab or newline character.


3 Answers

If you don't want add the /s regex modifier (perhaps you still want . to retain its original meaning elsewhere in the regex), you may also use a character class. One possibility:

[\S\s]

a character which is not a space or is a space. In other words, any character.

You can also change modifiers locally in a small part of the regex, like so:

(?s:.)
like image 70
ephemient Avatar answered Sep 23 '22 06:09

ephemient


Add the s modifier to your regex to cause . to match newlines:

$string =~ /(START)(.+?)(END)/s;
like image 40
BoltClock Avatar answered Sep 23 '22 06:09

BoltClock


Yeap, you just need to make . match newline :

$string =~ /(START)(.+?)(END)/s;
like image 10
FailedDev Avatar answered Sep 24 '22 06:09

FailedDev