Castro
Castro

Reputation: 11

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.

Upvotes: 0

Views: 637

Answers (2)

Stephan
Stephan

Reputation: 56180

just to offer an alternative:

#include <array.au3>
$sText = 'XXXXX 0,20 XXXXXXX 0,24 Button12 0,21 XXX'
$aResult=StringRegExp($sText, " \d{1,3},\d{2} ",3)
_ArrayDisplay($aResult)

extracts numbers with 1 to 3 digits before and exactly 2 digits after the comma enclosed in spaces (according to your description, there are some, but it also works, if you omit them in the pattern). Lenght of the "X"-strings doesn't matter.

Upvotes: 0

user5992808
user5992808

Reputation:

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

Upvotes: 2

Related Questions