Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex match including new line

Tags:

regex

ruby

I have a regex to grab everything between "*":

str = "Donec sed odio dui. *Nullam id dolor id nibh ultricies vehicula ut*"
str.match(/\*(.*)\*/)[1]

I want the match to be able to include newline characters. How can I do it?

like image 861
Jonnyx Delavilla Avatar asked Jul 12 '13 12:07

Jonnyx Delavilla


People also ask

What is \r and \n in regex?

Regex recognizes common escape sequences such as \n for newline, \t for tab, \r for carriage-return, \nnn for a up to 3-digit octal number, \xhh for a two-digit hex code, \uhhhh for a 4-digit Unicode, \uhhhhhhhh for a 8-digit Unicode.

Does dot match newline regex?

By default, a dot matches any character, except for the newline. That default can be changed to add matching the newline by using the single line modifier: for the entire regular expression with the /s modifier, or locally with (? s) (and even globally within the scope of use re '/s' ).

What does \r do in regex?

The \r metacharacter matches carriage return characters.

How do you match in regex?

Example : [^abc] will match any character except a,b,c . [first-last] – Character range: Matches any single character in the range from first to last.


2 Answers

You'll want to use the m option which allows the dot to match new lines:

Donec sed odio dui. *Nullam id dolor id 
nibh ultricies vehicula ut*

regex str.match(/\*(.*)\*/m)[1] Live example: http://www.rubular.com/r/11u9TreEOL

Your expression will capture the text between the first and last * symbol, but if you want to capture all text between each set of * then you'd want to make the .* less greedy like

str.match(/\*(.*?)\*/m)[1] Live example http://www.rubular.com/r/rBLOnwy3By

Or you could tell it to simply match all non * characters like, note the m option is not needed as a new line character would be matched by a the negated [^*] character class:

str.match(/\*([^*]*)\*/)[1] Live example http://www.rubular.com/r/dhQzZ58ZzM

like image 86
Ro Yo Mi Avatar answered Oct 22 '22 00:10

Ro Yo Mi


Put the m modifier after the regex like /\*(.*)\*/m.

By the way, your regex can be improved to:

str[/(?<=\*).*(?=\*)/m]
like image 23
sawa Avatar answered Oct 22 '22 00:10

sawa