Using regular expressions, how do I find a pattern surrounded by two other patterns without including surrounding lines?

I want to use regular expressions (Perl compatible) to be able to find a pattern surrounded by two other patterns, but not include strings matching surrounding patterns in the match.

For example, I want to be able to find occurrences of strings, for example:

Foo bar baz

But only coincidence includes the middle part:

Bar

I know that this is possible, but I do not remember how to do it.

+4
source share
4 answers

In the general case, you probably cannot. The easiest approach is to match everything and use backlinks to capture the part of interest:

Foo\s+(Bar)\s+Baz 

This is not the same as containing no surrounding text in the match. It probably doesn't matter if all you want to do is extract the β€œBar”, but it will matter if you match the same line multiple times and you need to continue from where you left off previous match.

Look-around will work in some cases. Tomalak's offer:

 (?<=Foo\s)Bar(?=\sBaz) 

only works with fixed width (at least in Perl). Starting in Perl 5.10, the \K statement can be used to efficiently represent variable widths:

 Foo\s+\KBar(?=\s+Baz) 

which should be able to do everything you requested in all cases, but will require that you implement this in Perl 5.10.

While it would be convenient, there is no \K equivalent to complete the agreed text, so you need to use the "look ahead" option.

+4
source

Brackets define groupings.

 "Foo (Bar) Baz" 

Example

 ~> cat test.pl $a = "The Foo Bar Baz was lass"; $a =~ m/Foo (Bar) Baz/; print $1,"\n"; ~> perl test.pl Bar 
+7
source

Use lookaround :

 (?<=Foo\s)Bar(?=\sBaz) 

This will match any β€œBar” preceded by β€œFoo” and then β€œBaz”, separated by a single space. "Foo" and "Baz" will not participate in the final match.

+4
source

$ string = ~ m / Foo (Bar) Baz /

$ 1

This may not be exactly what you want, because the match is still β€œFoo Bar Baz”. But it shows you how easy it is to get the part you are interested in. Otherwise, you can use lookahead and lookbehind to get matching without using characters ...

+2
source

Source: https://habr.com/ru/post/1277651/


All Articles