Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I ask for any and all monks whose knowledge of regular expressions exceeds my own to help me optimize this regex.
Optimize for speed or readability the choice is yours. I am parsing computer generated, badly formatted, HTML which has a number of lines that look like this
<tr> <td width="0" align="center"><font face="Arial" size="2">29022</font> </td><td width="0" align="center"><font face="Arial" size="2">01</font +> </td><td width="0" align="center"><font face="Arial" size="2">354</fon +t> </td><td width="0" align="center"><font face="Arial" size="2">201</fon +t> </td><td width="0" align="center"><font face="Arial" size="2">01</font +> </td><td width="0" align="center"><font face="Arial" size="2">&nbsp;</ +font> </td><td align="center"><font face="Arial" size="2">INTRODUCTION TO FI +LM</font> </td><td align="center"><font face="Arial" size="2"> 3</font> </td><td align="center"><font face="Arial" size="2">MW5M7,8</font> </td><td align="center"><font face="Arial" size="2">MI-100</font> </td><td align="center"><font face="Arial" size="2">&nbsp;</font> </td></tr>
The first piece of data is always 5 digits, the second 2, the third 3, the fourth 3, the fifth 2, the sixth a $nbsp or a single letter, the seventh an unspecified number of words, the eigth a single space followed by a digit, the ninth will always have 1 letter and then will either be another letter followed by a number or two numbers (that will possibly be followed by another similar set, as shown above), the tenth will always be letters followed by a dash and then some numbers or letters, and finally the eleventh can be ignored.

I am currently using /^<tr.*?(\d\d\d\d\d).*?<td.*?>(\d\d).*?<td.*?>(\d\d\d).*?<td.*?>(\d\d\d).*?<td.*?>(\d\d).*?<td\.*?>(?:&).*?<td.*?>(\w+(?:(?:\s\w+)?)*).*?<td.*?>(\s\d).*?<td.*?>(\w(?:(?:[\w\d,])?)*).*?<td.*?>(\w(?:\(?:[\w\d-])?)*)/ to pull out the relevant pieces of information. Any advice would be appreciated.

-Etan

P.S. Oh and if possible some slight explanation would be useful. I understand the basics of regex but flags and oddities still escape me. Thanks in advance.

P.P.S I had been checking for both the <td> and <font> tags but then read this node and realized that I was asking for too much. So I stopped asking for the font tags. Just thought I'd throw that in to put my thought processes up for inspection.

In reply to Regex optimization by deryni

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-28 23:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found