htmlregexperl

Perl Regex Pattern Matching


I want to use regex from a source file named source.html or source.txt:

<OPTION value=5>&nbsp;&nbsp;5 - Course Alpha (3)</OPTION> <OPTION value=6>&nbsp;&nbsp;6 - Course Beta (3)</OPTION>

to get:

5 - Course Alpha (3)
6 - Course Beta (3)

I mean I have to find a pattern:

<OPTION v

and

 finding first number after it 

so getting everything till I see:

</OPTION>

How can I implement it with Perl using Regex?

PS: It should read the content from a file and write output to a file.


Solution

  • perl -lwe '$_="<OPTION value=5>&nbsp;&nbsp;5 - Course Alpha (3)</OPTION> <OPTION value=6>&nbsp;&nbsp;6 - Course Beta (3)</OPTION>"; s/\&nbsp;//g; print $1 while /<OPTION [^>]*>([^<]+)/g'