Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaching for a String between two strings using regular expression in perl

Tags:

perl

i want to retrieve a string which falls between two specified strings multiple times in a file

i tried this, but this doesnt work

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

i want every thing that falls between "String 1" and "String 2"

Please help

like image 490
Vidur Oberoi Avatar asked Feb 17 '23 15:02

Vidur Oberoi


1 Answers

Assuming your input string is like this

$str='String 1GIANT FISHString 2'

this will work

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

$wanted is now "GIANT FISH"

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

ok with multiline, assuming input of

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

this will get all the strings

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

@wanted has three entries

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

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

like image 160
Vorsprung Avatar answered May 18 '23 17:05

Vorsprung