?
Solved

Need help on regex

Posted on 2011-05-06
4
Medium Priority
?
301 Views
Last Modified: 2012-05-11
Hi experts,

I need to do a regex to get the MPN number that's contained in a table. The table is basically a two column table with heading such as Manufacturer, Warranty etc while the second column displays the data for the heading. An example of the html of the table is listed below:

<td class="label">Manufacture Warranty</td><td class="data">1 year</td></tr><tr><td class="label">MPN</td><td class="data">LE40C750</td></tr><tr><td class="label">Optical Audio Out</td><td class="data">No</td></tr><tr><td class="label">PC Slot</td><td class="data">Yes</td></tr><tr><td class="label">Scart Connection</td><td class="data">2</td></tr><tr><td class="label">Screen Refresh Rate</td><td class="data">200 Hz</td></tr><tr><td class="label">Screen Type</td><td class="data">3D</td></tr><tr><td class="label">USB Connection</td><td class="data">Yes</td></tr></tbody>

I've tried to do a regex like this:

(MPN</td><td class="data">)(.*)(</td></tr><tr><td class="label">?)
$2

But that gets this:

"LE40C750</td></tr><tr><td class="label">Optical Audio Out</td><td class="data">No</td></tr><tr><td class="label">PC Slot</td><td class="data">Yes</td></tr><tr><td class="label">Scart Connection</td><td class="data">2</td></tr><tr><td class="label">Screen Refresh Rate</td><td class="data">200 Hz</td></tr><tr><td class="label">Screen Type</td><td class="data">3D"

But what i want is:

"LE40C750"

I know it's probably an easy answer but I've run out of ideas
0
Comment
Question by:AnuarJamlus
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 35707262
You're using a "greedy" star. Dot-star will try to match as much as possible, so in your pattern the dot-star will read up to and including the last occurrenct of:

(</td></tr><tr><td class="label">?)

Open in new window


Try making it non-greedy by appending a question mark to the dot-star:
(MPN</td><td class="data">)(.*?)(</td></tr><tr><td class="label">?)

Open in new window

0
 
LVL 1

Author Closing Comment

by:AnuarJamlus
ID: 35707276
Perfect. And the explanation certainly made me understand it better.

Thanks!
0
 
LVL 4

Expert Comment

by:a1j
ID: 35707285
Try nongreedy match in regexp (note question mark after *)
(MPN</td><td class="data">)(.*?)(</td></tr><tr><td class="label">?)


or use more explicit match like this
(MPN</td><td class="data">)([^<]*)(</td></tr><tr><td class="label">?)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35707287
NP. Glad to help  = )
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question