Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can regex match be based on two lines of text?

Tags:

regex

Let's say I have

def
abc
xyz
abc

And I want to match

xyz
abc

as a whole

Is this possible using the most generic RegEx possible? That is not the perl RegEx or .Net Regex which have multi line flags.

I guess it would be BNF to match this.

like image 965
Matt Avatar asked Jun 02 '10 22:06

Matt


2 Answers

Many regex implementations allow explicit line terminators. If \n is the line separator, then just search for xyz\nabc.

like image 176
wallyk Avatar answered Sep 19 '22 02:09

wallyk


Regexes work on whatever text you give them, multiline or otherwise. If it happens to contain linefeeds, then it's nominally "multiline" text, but you don't have to do anything special to match it with regexes. Linefeed is just another character.

The name "multiline flag" (or "multiline mode") confuses many people. All that flag does is change the meaning of the ^ and $ anchors, allowing them to match at the beginning and end of logical lines as well as the beginning and end of the whole text.

like image 23
Alan Moore Avatar answered Sep 20 '22 02:09

Alan Moore