I'm trying to familiarize myself a little with Perl to use for regular expression searches in Terminal (Mac). Now, I'm not really looking to learn Perl rigourously, just trying to find out how to do some simple regular expressions.
But I can't figure out how to do this in Terminal:
I'd like to be able to match expressions over several lines, and I'll take HTML tags as an example. PLEASE NOTE, that the HTML tag is just an example of something to match, and specifically something that goes over multiple lines. Whether matching HTML with regular expressionS is a good idea or not is not the issue. I just want to understand the syntax of matching with Perl on the command line!
Say I want to match the entire ul tag here:
<ul> <li>item 1</li> <li>item 2</li> </ul>
I would like to:
- Be able to match this in a file and output the match to the stdout (don't ask why, I would just want to to understand how it works :-))
- Be able to replace it with something else.
For matching, I found something like this (using 'start' and 'end' as an example here from a simple text file when I was testing, but please give the example for the
ul tag instead:
perl -wnE 'say $1 if /(start(.*?)end)/' test.txt
This matches a part, but only on one line. Surprisingly, adding the s at the end didn't work to make it "dotall" or "single-line mode", it still just matched one line...
For replacing, I tried something like this:
perl -pe 's/start(.*?)end/replacement text/'s test.txt
This didn't work either...