Allocating a line from two lines using regex in perl

I want to get a line that falls between two specified lines multiple times in a file

I tried this but it doesn't work

/(?m)"String 1"!.*?"String2":/;

      

I want every thing that is between "Line 1" and "Line 2"

Please, help

+3


source to share


2 answers


Assuming your input string is like this

$str='String 1GIANT FISHString 2'

      

it will work

($wanted)= $str =~ /String 1(.*)String 2/

      

$wanted

now "GIANT FISH"

dah..multiline in file ... edit coming up

ok with multiline, assuming input



String 1Line oneString 2
String 1GIANT FISHString 2
String 1String2

      

this will get all lines

(@wanted)= $str =~ /String 1(.*)String 2/g

      

@wanted has three entries

('Line one','GIANT FISH','')

      

In the second regex, g for globals finds all matches in the string

+7


source


Below:

perl -lne 'push @a,/string(.*?)string/g;END{print "@a"}'

      

the two strings are a string and a string, and whatever lies in between will look like an array element. Below is an example I tested for this purpose. You can change the two lines in any way that you need the line in.



tested:

> cat temp
string123stringstring234string
string456stringstring789string

> perl -lne 'push @a,/string(.*?)string/g;END{print "@a"}' temp
123 234 456 789

      

0


source







All Articles