Solved

Regular expression

Posted on 2014-03-30
2
410 Views
Last Modified: 2014-03-30
Looks like my understanding of negative lookaheads is lacking.

I have this test string:
<tr>
<td>text</td>
<td></td>
<td>code</td>
</tr>
<tr>
<td>text</td>
</tr>
<tr>
<code>
</tr>

Open in new window

and the following expression:
(?sm)<tr>.*?(text).*?(code).*?</tr>

Open in new window


I want to match "text" followed by "code" in the same row.
That is, "code" should follow "text" before "<\tr>".
In the sample, the only match should be
<tr>
<td>text</td>
<td></td>
<td>code</td>
</tr>

Open in new window


How can I write the regular expression for that?
0
Comment
Question by:Dan Craciun
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39965500
I believe this is what you are after:

(?sm)<tr>.*?(text)(?:.(?!</tr>))+?(code)

Open in new window


Basically, you simply look ahead of the current character matched by the dot, and you check to make sure that the following text is not the closing row tag. There may be a more elegant way to do it, but this should suffice for your current need.

You may find my article on regular expression lookaround useful:  http://www.experts-exchange.com/Programming/Languages/Regular_Expressions/A_4318-Regular-Expression-Lookaround-Demystified.html
0
 
LVL 34

Author Closing Comment

by:Dan Craciun
ID: 39965719
Yup.
I kept writing the lookahead as
(?:.*(?!</tr>))
and it refused to work.

Thank you, kaufmed.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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,…
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…

776 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