Venkat
Venkat

Reputation: 181

perl Match multiline pattern in a file

I have a program which reads a file line by line and gets data when a pattern is matched.
currently it extracts patterns like
function abc (int a, int b)
but i have some functions like
function xyz (int a, \n
int b)

which it doesn't match due to file being read line by line.

Is it possible to read the file in some better way or should i use the obvious technique of getting multiple lines.

Upvotes: 0

Views: 2362

Answers (1)

Sysyphus
Sysyphus

Reputation: 1061

You'll need to read multiple lines in at once. If the file isn't too large you can just slurp the entire file in (e.g. http://www.perl.com/pub/2003/11/21/slurp.html) and then use a single line regex (use the s option e.g. /stuff.next line/s).

editExample for multiple matches* The g option allows you to get all the matches. One sample usage is in a while loop, where each time you evaluate the regex you get the next match. See Perl iterate through each match for more details and examples.

while($string=~/(regex)/g){
    DoSomething($1);
}

edit fixed error

Upvotes: 2

Related Questions