从文本中提取数字信息

时间:2017-07-14 08:24:01

标签: regex pcre autoit

我需要从文本中提取数字信息。

Ready

State: CTYG  Work Request #: 2880087 General

Job Address

Contact

Work Request Search

我的代码:

$Text = WinGetText("[ACTIVE]")
Sleep(4000)
$Value = StringSplit($Text, @CRLF)
MsgBox(0, "Hello", $Value, 10) ;---1st message box
Sleep(4000)

For $i = 1 To $Value[0]

    If StringRegExp($Value[$i], "[0-9][^:alpha:]") Then

        MsgBox(0, "Hello1", $Value[$i], 5) ;---2nd message box
        Sleep(200)
        $newWR = $Value[$i]
        MsgBox(0, "Hello2", $newWR, 10)
        ConsoleWrite($newWR) ;---3rd message box

    EndIf

Next

第一MsgBox()没有显示任何内容。第二和第三节目State: CTYG Work Request #: 2880087 General。但我不需要整行,我只想要2880087

2 个答案:

答案 0 :(得分:0)

这个怎么样?这将删除除数字之外的所有内容。

$str = "State: CTYG Work Request #: 2880087 General"
ConsoleWrite(StringRegExpReplace($str, '\D', '') & @CRLF)

答案 1 :(得分:0)

  

... 我只想要2880087

使用正则表达式State: .+ #: (\d+)的示例:

#include <StringConstants.au3>; StringRegExp()
#include <Array.au3>

Global Const $g_sText   = 'Ready' & @CRLF & @CRLF _
                        & 'State: CTYG Work Request #: 2880087 General' & @CRLF & @CRLF _
                        & 'Job Address' & @CRLF & @CRLF _
                        & 'Contact' & @CRLF & @CRLF _
                        & 'Work Request Search'
Global Const $g_sRegEx  = 'State: .+ #: (\d+)'
Global Const $g_aResult = StringRegExp($g_sText, $g_sRegEx, $STR_REGEXPARRAYMATCH)

ConsoleWrite($g_sText & @CRLF)
_ArrayDisplay($g_aResult)

2880087存储到$g_aResult[0]