开发者

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.


You do not want to use a regex, you want to use an HTML parser. Here's a good article on the subject which explains why regexes are fragile and how to use HTML::TreeBuilder.

There's also a small pile of similar questions and answers about extracting data from HTML documents.


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'


What about

/<OPTION v.*?>.*?(\d.+?)<\/OPTION>/

http://regexr.com?2thm8

There you will find your strings in the first capturing group.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜