Jump to content

Recommended Posts

Posted

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>")

Posted

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)

 

Posted

Say I have many different symbols/characters in between <td> and </td> but all I want to identify is if fox or cat is in between them, while ignoring everything else.

Posted (edited)

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
Posted (edited)

This should be possible with stringregexp() right? Or can this be done with a different function like IE UDF?

Edited by element72
Posted (edited)

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
Posted

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)

 

Posted

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. 

Posted (edited)

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
Posted

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:

Posted

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

 

Posted

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?

Posted
#Include <Array.au3>
$HTML = "<td>aaaa <b>©FOX</b>aaa ttt</td> <td>aaacat bbbb aaa</td> <td> <i><strong>CAT</i></strong>dssfox df sdf</td>"

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

Using 2 passes as said jguinch could be a more robust solution though, should be tested

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
  • Recently Browsing   0 members

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