Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match all text between two strings multiline

Tags:

regex

I'm trying to accomplish the same thing as seen here:

i.e. assuming you have a text like:

<p>something</p>

<!-- OPTIONAL -->

<p class="sdf"> some text</p>
<p> some other text</p>

<!-- OPTIONAL END -->

<p>The end</p>

What is the regex that would match:

<p class="sdf"> some text</p>
<p> some other text</p>

I've setup a live test here using:

<!-- OPTIONAL -->(.*?)<!-- OPTIONAL END -->

but it's not matching correctly. Also the accepted answer on the page didn't work for me. What am I missing?

like image 771
rtuner Avatar asked Jun 23 '14 22:06

rtuner


People also ask

What is multiline matching?

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.

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

So use \s\S, which will match ALL characters.

How do you match multiple lines in Python?

The re. MULTILINE flag tells python to make the '^' and '$' special characters match the start or end of any line within a string. Using this flag: >>> match = re.search(r'^It has.

What is multiline flag in regex?

The m flag indicates that a multiline input string should be treated as multiple lines. For example, if m is used, ^ and $ change from matching at only the start or end of the entire string to the start or end of any line within the string. You cannot change this property directly.


1 Answers

Well unfortunately, RegExr is dependent on the JS RegExp implementation, which does not support the option to enable the flag/modifier that you need.

You are looking for the s (DotAll) modifier forcing the dot . to match newline sequences.

  • Live Demo on regular expressions 101

If you are using JavaScript, you can use this workaround:

/<!-- OPTIONAL -->([\S\s]*?)<!-- OPTIONAL END -->/
like image 198
hwnd Avatar answered Oct 21 '22 15:10

hwnd