简体   繁体   中英

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.

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 (eg http://www.perl.com/pub/2003/11/21/slurp.html ) and then use a single line regex (use the s option eg /stuff.next line/s).

edit Example 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM