Jump to content

Help Needed: Filtering text:


Vowles
 Share

Recommended Posts

Heres my problem/goal:

I have a webpage, It contains details, It is set out like this:

<tr>
    <td><b>Callsign:</b></td>
    <td align="right">Vowlesy</td>
</tr>

Now, I want to extract "Vowlesy" from this,and then apply the extracted text to a variable. ($Callsign?).

So I need the first set of <td>/</td> to see which $ to place the second set of <td>s vaule.

Iam quite well knowing in how AutoIT works but no where near can use it to its full yet.

All and any help on this will be greatly apprecitated.

Thanks

Vowles

Link to comment
Share on other sites

I hope thats help:

$get_source = _InetGetSource("Your webpage")
$Callsign = _StringBetween($get_source, 'td align="right">', '</td>')
MsgBox(0, "", "Callsign: " &$Callsign)

Func _StringBetween($s,$from,$to)
$x=StringInStr($s,$from)+StringLen($from)
$y=StringInStr(StringTrimLeft($s,$x),$to)
Return StringMid($s,$x,$y)
EndFunc
Link to comment
Share on other sites

maybe like this

Dim $result_3 = ""

$test = '<td align="right">Vowlesy</td>'

$L_loc = StringInStr($test, '">')

$result = StringTrimLeft( $test, $L_loc +1)

$R_loc = StringInStr($result, '</', 0, +1)

$result_2 = StringLeft( $result, $R_loc -1 )

If $result_2 <> $result_3 Then
    $result_3 = $result_2
;write the line to the file
    MsgBox(0,"test", "final result = " & $result_3)
EndIf
$Callsign =  $result_3

8)

NEWHeader1.png

Link to comment
Share on other sites

Here's another approach using StringRegExp() (available in the beta):

; You'd probably read this from a file or something
Local $Data = '<tr>' & @LF & '<td><b>Callsign:</b></td>' & @LF & '<td align="right">Vowlesy</td>' & @LF & '</tr>'
Local $RegExp = '<b>(.*?):</b>.*?<td align="right">(.+?)</td>'

Local $Tokens = StringRegExp($Data, $RegExp, 3)
; $Tokens[0] contains the first TD value minus the colon
; $Tokens[1] contains the second TD value

Assign($Tokens[0], $Tokens[1], 1)
MsgBox(0x40, '$Callsign', $Callsign)
Link to comment
Share on other sites

It really depends on how empty values should be handled.

My pattern will cause the match to fail for such a case (which could then be @Error-trapped to ignore that case), whereas your case (after some pending bug fixes to StringRegExp()) would return a blank value, which would cause the variable to be defined but empty... which is probably desired actually.

Link to comment
Share on other sites

It almost is. B)

Local $RegExp = '<b>(.*?):</b>.*?<td align="right">(.+?)</td>'
Local $Tokens = StringRegExp($Data, $RegExp, 3)

The above code roughly translates to the following English:

I expect $data to contain a <b> tag, followed by some undefined number of characters, followed by a colon and a </b>, followed by any amount of characters, followed by <td align="right">, followed by one or more characters, followed by </td>. If $data does in fact fit that description then I want you to grab the bit between the <b> and the :</b> (i.e. the bit in the brackets) and the bit between the <td> tags (again in brackets) and return those two items as an array.

Regular expressions are a powerful tool (and I need to have them properly learnt before my exam tomorrow!), especially in this kind of case where you want to extract pieces of information from a longer string. It's great to have this functionality in AutoIt now and if one plans on doing any string manipulation like the above example, it's worthwhile to learn the basics.

Link to comment
Share on other sites

It almost is. B)

I need to have them properly learnt before my exam tomorrow!)

AutoIt now and if one plans on doing any string manipulation like the above example, it's worthwhile to learn the basics.

looks like you learnt it midy fine

learn a new language... ah

guess i will have too ... sooner or later

8)

NEWHeader1.png

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