Sun, 22 May 2011

Perl Multi-Line Regex and Line Breaks

After a co-worker asked for some help in matching and deleting from a file all occurances of:

    delete: uid

Our confusion stemmed from the fact that even with the /m flag to regular expressions, ^ and $ still only can anchor the beginning and end of a regex — to match internal line breaks you need '\n'. E.g.

perl -p -i.orig -000 -e 's/\n-\ndelete: uid$//mg' file

Posted at: 19:46 | category: /computers/perl | Link