Jump to content

What is the 'pattern' I need for stringregexp


element72
 Share

Recommended Posts

Say I'm looking at an html source that somewhere contains <td>aaaa ©FOXaaa ttt</td> <td>aaacat bbbb aaa</td> <td>CATdss foxdf sdf</td>.

I want to capture fox (or cat) but in between <td> and </td>.

I just keep getting it wrong. My best effort is $array = StringRegExp($HTML, "(?i)<td>([(cat)(fox)]?)</td>")

Link to comment
Share on other sites

There are better ways to parse html than regex, this is the first way I have it working.  The RegEx pro's are sure to come post some better ways.

 

$sString = "<td>aaaa ©FOXaaa ttt</td> <td>aaacat bbbb aaa</td> <td>CATdss foxdf sdf</td>"

$sNewString = StringRegExpReplace($sString, "(?i)<td>(.*?)</td>", "$1")

MsgBox(0, "", $sNewString)

 

Link to comment
Share on other sites

Yeah I thought you wanted everything between <td> I need to fix it.

Almost not quite, seems it will not pick up the repeats present between <td> if its more than once.

#Include <Array.au3>

$sString = "<td>aaaa ©FOXaaa ttt</td> <td>aaacat bbbb aaa</td> <td>CATdss foxdf sdf</td>"

$aNewString = StringRegExp($sString, "(?i)<td>.*?(cat|fox).*?</td>", 3)

_ArrayDisplay($aNewString)

 

Edited by ViciousXUSMC
Link to comment
Share on other sites

This ?

#Include <Array.au3>
$HTML = "<td>aaaa ©FOXaaa ttt</td> <td>aaacat bbbb aaa</td> <td>CATdss foxdf sdf</td>"

$array = StringRegExp($HTML, "(?i)<td>[^<]*(cat|fox)[^<]*</td>", 3)

_ArrayDisplay($array)

 

Link to comment
Share on other sites

This ?

#Include <Array.au3>
$HTML = "<td>aaaa ©FOXaaa ttt</td> <td>aaacat bbbb aaa</td> <td>CATdss foxdf sdf</td>"

$array = StringRegExp($HTML, "(?i)<td>[^<]*(cat|fox)[^<]*</td>", 3)

_ArrayDisplay($array)

 

Its missing the last set where both cat & fox are in a single string between <td></td>  I tried to look online how to "repeat" the capture but could not quite figure it out.  I assume some kind of back reference or something would be needed. Been eyeing this thread myself since I could not figure it out.

Closest I found was this, but none of it worked: http://www.regular-expressions.info/captureall.html

 

Edited by ViciousXUSMC
Link to comment
Share on other sites

Sorry, I didn't see the last elems contains two results.

Try this one :

#Include <Array.au3>
$HTML = "<td>aaaa ©FOXaaa ttt</td> <td>aaacat bbbb aaa</td> <td>CATdss foxdf sdf</td>"

$aRes = StringRegExp($HTML, "(?i)(fox|cat)(?=[^<]*<\/td>)", 3)
_ArrayDisplay($aRes)

 

Link to comment
Share on other sites

Sorry, I didn't see the last elems contains two results.

Try this one :

#Include <Array.au3>
$HTML = "<td>aaaa ©FOXaaa ttt</td> <td>aaacat bbbb aaa</td> <td>CATdss foxdf sdf</td>"

$aRes = StringRegExp($HTML, "(?i)(fox|cat)(?=[^<]*<\/td>)", 3)
_ArrayDisplay($aRes)

 

Works :) not sure how lol.

(?i) Case Insensitive
(fox|cat) match & capture fox or cat
(?=[^<]*<\/td>) look ahead and match but do not capture Not < 0 or more times and </td> one time.

I just do not understand how this one picks up the duplicate cat/fox matches when the other regex was not able to.  I'll save this for notes though. 

Link to comment
Share on other sites

ViciousXUSMC,

Hehe  :)

"(fox|cat)[^<]*</td>"  => matches a string containing fox|cat AND zero or more non-< chars up to </td>
When done, the search continues after the </td>

"(fox|cat)(?=[^<]*<\/td>)"  => matches fox|cat followed by 0 or more non-< chars and </td>
When done, the search continues after the match
It's the magic of the lookahead which is a zero-length assertion  

BTW [^<]* is used to check that there is no html tag between fox|cat and </td>

Edit :
Note that you can also do it like this "(?i)(?=(fox|cat)[^<]*</td>)"   :D

Edited by mikell
Link to comment
Share on other sites

Yes, for me this kind of regex should be done in two pass for more flexibility :

#Include <Array.au3>
$HTML = "<td>aaaa ©FOXaaa ttt</td> <td>aaacat bbbb aaa</td> <td>CATdss foxdf sdf</td>"

For $elem In StringRegExp($HTML, "<td>(.+?)</td>", 3)
    $aRes = StringRegExp($elem, "(?i)(cat|fox)", 3)
    If IsArray($aRes) Then _ArrayDisplay($aRes)
Next

 

Link to comment
Share on other sites

Hmmmyes, but as usual in such topics about regex the OP doesn't provide enough details concerning the source... thus the regex fails if there are html font tags : <td><b>CATdss foxdf sdf</b></td>  :ermm:

good point. How would I solve this kind of problem? or include that into the regexp?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...