Jump to content

StringRegExp


Recommended Posts

I made the following code:

$String="<td class=nm>Sigourney Weaver</td><td class=char>Ellen Ripley</td><td class=nm>Carrie Henn</td><td class=char>Rebecca 'Newt' Jorden</td><td class=nm>Michael Biehn</td><td class=char>Cpl. Dwayne Hicks</td>"

$String=StringReplace($String,"</td><td class=char>",chr(32) & "(",0)
$String=StringReplace($String,"</td><td class=nm>",")" & chr(32) & "/" & chr(32),0)
$String=StringReplace($String,"<td class=nm>","",0)
$String=StringReplace($String,"</td>",")",0)
$String=StringStripWS($String,7)

Msgbox(0,"Cast","*" & $String & "*")

This code works good, but I'm not happy with it.

I'm not an expert for regular expressions.

Is it possible make a shorter code with the StringRegExp function?

Please help me.

Link to comment
Share on other sites

  • Moderators

You're doing 4 different things to each of those (4 different replacement values), you're safer doing what you are doing (but yes, it's possible).... you could split it up the same way with StringRegExpReplace() the same way as you have with StringReplace() and you'll find a speed increase.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

This code works good, but I'm not happy with it.

I'm not an expert for regular expressions.

Is it possible make a shorter code with the StringRegExp function?

Please help me.

Use the following example from the StringRegExp example in Help.

;Option 2, single return, php/preg_match() style
$array = StringRegExp('<test>a</test> <test>b</test> <test>c</Test>', '<(?i)test>(.*?)</(?i)test>', 2)
for $i = 0 to UBound($array) - 1
    msgbox(0, "RegExp Test with Option 2 - " & $i, $array[$i])
Next

If you still can't get it let us know. :)

Link to comment
Share on other sites

I think this may be along the lines of what you are looking for.

$String = "<td class=nm>Sigourney Weaver</td><td class=char>Ellen Ripley</td><td class=nm>Carrie Henn</td><td class=char>Rebecca 'Newt' Jorden</td><td class=nm>Michael Biehn</td><td class=char>Cpl. Dwayne Hicks</td>"
$nOffset = 1
$Cast = ""
While 1
    $Name = StringRegExp($String, "<td class=nm>(.*?)</td>", 1, $nOffset)
    If @error = 0 Then
        $nOffset = @extended
    Else
        ExitLoop
    EndIf
    $Char = StringRegExp($String, "<td class=char>(.*?)</td>", 1, $nOffset)
    If @error = 0 Then
        $nOffset = @extended
    Else
        ExitLoop
    EndIf
    If $Cast <> "" Then $Cast = $Cast & " / "
    $Cast = $Cast & $Name[0] & " (" & $Char[0] & ")"
WEnd
MsgBox(0, "Cast", "*" & $Cast & "*")
Link to comment
Share on other sites

Another possibility you could try is:

#include <String.au3>
Dim $aArray1[1], $cast

$String="<td class=nm>Sigourney Weaver</td><td class=char>Ellen Ripley</td><td class=nm>Carrie Henn</td><td class=char>Rebecca 'Newt' Jorden</td><td class=nm>Michael Biehn</td><td class=char>Cpl. Dwayne Hicks</td>"

$String = StringReplace ( $String, "<td class=char>", "<td class=nm>")
$aArray1 = _StringBetween($String, '<td class=nm>', '</td>')

For $i = 0 to UBound($aArray1, 1) -1
    $cast &= $aArray1[$i] & ", "
Next
$cast = StringTrimRight($cast, 2)

Msgbox(0,"Cast 0","* " & $cast & " *")oÝ÷ Ù8^­íý²;¬¶Ê&j|°k+-¢É"Øb±.r¥uëÞZ()í¢Ø^ F®Ýû§rبȧqëajܱ׬    ÞvÚ)ºYbú+«b¢wë¢f°yºZæÄv+lêi³}÷ß
è(uëaȬ­"gÛh+ÞÊ¢ºòßv®¶­sb6æ6ÇVFRfÇCµ7G&æræS2fwC° ¤FÒb33c¶'&³ÒÂb33c¶'&%³ÒÂb33c¶67@¢b33cµ7G&æsÒgV÷C²fÇC·FB6Æ73ÖæÒfwCµ6v÷W&æWvVfW"fÇC²÷FBfwC²fÇC·FB6Æ73Ö6"fwC´VÆÆVâ&ÆWfÇC²÷FBfwC²fÇC·FB6Æ73ÖæÒfwC´6'&RVæâfÇC²÷FBfwC²fÇC·FB6Æ73Ö6"fwCµ&V&V66b33´æWwBb33²¦÷&FVâfÇC²÷FBfwC²fÇC·FB6Æ73ÖæÒfwC´Ö6VÂ&VâfÇC²÷FBfwC²fÇC·FB6Æ73Ö6"fwC´7ÂâGvæR6·2fÇC²÷FBfwC²gV÷C° ¢b33c¶'&Òõ7G&æt&WGvVVâb33cµ7G&ærÂb33²fÇC·FB6Æ73ÖæÒfwC²b33²Âb33²fÇC²÷FBfwC²b33²¢b33c¶'&"Òõ7G&æt&WGvVVâb33cµ7G&ærÂb33²fÇC·FB6Æ73Ö6"fwC²b33²Âb33²fÇC²÷FBfwC²b33² ¤f÷"b33c¶ÒFòT&÷VæBb33c¶'&ÂÓ b33c¶67Bf׳Òb33c¶'&²b33c¶ÒfײgV÷C²gV÷C²fײb33c¶'&%²b33c¶ÒfײgV÷C²ògV÷C°¤æW@¢b33c¶67BÒ7G&æuG&Õ&vBb33c¶67BÂB ¤×6v&÷ÂgV÷C´67BgV÷C²ÂgV÷C²¢gV÷C²fײb33c¶67BfײgV÷C²¢gV÷C²
Edited by ssubirias3
Link to comment
Share on other sites

  • Moderators

So many ways is right... if you're just trying to use StringRegExp as everyone here is demonstrating... you could also do something like this:

$String="<td class=nm>Sigourney Weaver</td><td class=char>Ellen Ripley</td><td class=nm>Carrie Henn</td><td class=char>Rebecca 'Newt' Jorden</td><td class=nm>Michael Biehn</td><td class=char>Cpl. Dwayne Hicks</td>"
Dim $sHold = "*", $aArray
$aArray = StringRegExp($String, "(?s)(?i)\<td class.*?>(.*?)\<", 3)
For $iCC = 0 To UBound($aArray) - 1 Step 2
    $sHold &= $aArray[$iCC] & " (" & $aArray[$iCC+1] & " ) / "
Next
$sHold = StringTrimRight($sHold, 2) & "*"
MsgBox(0,0, $sHold)

But unless you want to get into the complication (I for one don't feel like trying to figure it out personally) of back tracking through StringRegExpReplace(), I still like the way you did it originally.

Edit:

Had to fix something...

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Puh, I just managed it. This an another alternative:

Dim $Array_1
Dim $Array_2
Dim $Cast
Dim $CastList

$String="<td class=nm>Sigourney Weaver</td><td class=char>Ellen Ripley</td><td class=nm>Carrie Henn</td><td class=char>Rebecca 'Newt' Jorden</td><td class=nm>Michael Biehn</td><td class=char>Cpl. Dwayne Hicks</td>"

$Array_1=StringRegExp($String,'<td class=nm>(.*?)</td>',3)
$Array_2=StringRegExp($String,'<td class=char>(.*?)</td>',3)
for $i=0 to UBound($Array_1)-1
    $Cast=$Array_1[$i] & chr(32) & "(" & $Array_2[$i] & ")" & chr(32) & "/" & chr(32)
    $CastList=$CastList & $Cast
Next
$CastList=StringTrimRight($CastList,3)
MsgBox(0,"Cast List","*" & $CastList & "*")

Yes, this a complex code :)

I thank you for your help!

Edited by Starfighter
Link to comment
Share on other sites

Glad we were able to help and you got it working! Your last option has some stuff that's not necessary imo. You'll notice that in your particular case StringBetween and StringRegExp are doing the same thing. I took my earlier code and replaced oe for the other for illustration.

Dim $Array1, $Array2, $Cast

$String="<td class=nm>Sigourney Weaver</td><td class=char>Ellen Ripley</td><td class=nm>Carrie Henn</td><td class=char>Rebecca 'Newt' Jorden</td><td class=nm>Michael Biehn</td><td class=char>Cpl. Dwayne Hicks</td>"

$Array1 = StringRegExp($String,'<td class=nm>(.*?)</td>',3)
$Array2 = StringRegExp($String,'<td class=char>(.*?)</td>',3)

For $i = 0 to UBound($Array1, 1) -1
    $Cast &= $Array1[$i] & " (" & $Array2[$i] & ") / "
Next

$Cast = StringTrimRight($Cast, 4)
MsgBox(0,"Cast List","* " & $Cast & " *")
Link to comment
Share on other sites

  • Moderators

Both of you need to learn about escape characters... from looking at your examples.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Both of you need to learn about escape characters... from looking at your examples.

Escape Characters: a single character which in a sequence of characters signifies that what is to follow takes an alternative interpretation.

I guess you're suggesting that we learn about the character sets that begin with "\" in order to shorter the StringRegExp lines. Well I've tried several combination of these escape character sets individually and together and can't figure out the right ones to i) produce the same result and ii) shorten the following lines. Granted they could be useful in shortening the pattern, however to a noob they can be quite cryptic and less focused allowing room for possible false positives.

$Array_1 = StringRegExp($String,'<td class=nm>(.*?)</td>',3)
$Array_2 = StringRegExp($String,'<td class=char>(.*?)</td>',3)

On the other hand if I've totally missed what you were suggestion, can you explain with an example. Thanks again for the edumakation!

Link to comment
Share on other sites

Array_1 = StringRegExp($String,'\<td class=nm>(.*?)\<\/td>',3)oÝ÷ ØZè§!j¶µêÜ¢éí¶¶×±jjey§]³w!j¶µêì¡ëuاØ^ªê-ÓݪºZ§¬qª^r«iË^®)íéÊZjب·­µêíç(+©iØ}êÞÇ­©ç¢Û¬x-ç¬qª^r«iË^¬êâ²Øb±©^²Ê'¢zk¢«³)ík"Ç°«mç¬qª^r«iË^®++y©eÊ×¥)àJÚâ^Li¶µÈmæ¶æ¥r«iË^®Ê®¢Ùmªê-jwjºªê-{az­µêç!ûazË ë-êº^©
Link to comment
Share on other sites

  • Moderators

\<\td class

should be

\<td class

\t is for "tab"

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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...