Search code examples
stringautoit

How to extract numbers from this string?


I use AutoIt on a flash site and read its text using Capture2Text. Some If -construct checks if a certain button is present. I get text like this:

XXXXX 0,20 XXXXXXX 0,24  Button12 0,21 XXX

X are fixed-size characters. I need the 3 numbers in variables. You can ignore Button12 (also a fixed size). The result should be:

$Var1 = 0,20
$Var2 = 0,24
$Var3 = 0,21

Value is between 0,20 and 900,00 (so no fixed size) and I need to get it without spaces etc.


Solution

  • For the given kind of text (your X can be any "word" characters) it works so:

    $sText = 'XXXXX 0,20 XXXXXXX 0,24 Button12 0,21 XXX'
    
    $aMatch = StringRegExp($sText, '\w+\s(\d+,\d+)\s\w+\s(\d+,\d+)\s\w+\s(\d+,\d+)', 3)
    If Not @error Then
        For $i = 0 To UBound($aMatch) -1
            ConsoleWrite('Var ' & $i+1 & ': ' & $aMatch[$i] & @CRLF)
        Next
    EndIf