2

特定の文字列内のすべての数値を取得するにはどうすればよいですか? それらが浮動小数点数、整数、正または負であるかどうかは問題ではありません。番号付きのキャプチャとして、それぞれ 50 または 100.25 または 12345678 または -78.999 をキャプチャする必要があります。

私の意図は、文字列 (autohotkey) 内の n 番目の番号を見つけて置き換えることです。

正規表現は、すべての一致を配列にキャプチャする必要があります。

これまでのところ、私はこの正規表現を思いつきました(これは最初の一致のみをキャプチャしているようです):

[-+]?\d+(\.\d+)?

興味があれば、これが私のオートホットキー機能です。

ReplaceNumber(whattext, instance, replacewith){
    numpos := regexmatch(whattext, "Ox)[-+]?\d+(\.\d+)?", thisnumber)
    returnthis := thisnumber.value(instance)
    return returnthis
}
4

2 に答える 2

1

ポリエチレンのgrep関数を使用すると、正規表現文字列を指定でき、すべての一致の区切り文字列が返されます。次に、その数値の正確なインスタンスを文字列に置き換えることができます。このスレッド(HamZa DzCyber​​DeVのおかげで)には、これがなぜであるかについての説明があります。

(これにはgrepスクリプトが必要です!)

ReplaceNumber(whattext, instance, replacewith){
    numpos := grep(whattext, "[+-]?\d+(?:\.\d+)?",thisnumber,1,0,"|")
    stringsplit, numpos, numpos,|
    stringsplit, thisnumber,thisnumber,|

    thispos := numpos%instance%   ;get the position of the capture
    thisinstance := thisnumber%instance%  ;get the capture itself
    thislen := strlen(thisinstance) 
    ;now fetch the string that comes before the named instance
    leftstring := substr(whattext, 1, thispos-1)
    rightstring := substr(whattext, thispos+thislen, strlen(whattext))

    returnthis := leftstring . replacewith . rightstring

    return returnthis
}
msgbox, % replacenumber("7 men swap 55.2 or 55.2 for 100 and -100.", 5, "SWAPPED")

結果:

; 1-->  SWAPPED men swap 55.2 for 100 and -100.
; 2-->  7 men swap SWAPPED or 55.2 for 100 and -100.
; 3 --> 7 men swap 55.2 or SWAPPED for 100 and -100.
; 4 --> 7 men swap 55.2 or 55.2 for SWAPPED and -100.
; 5 --> 7 men swap 55.2 or 55.2 for 100 and SWAPPED.

ありがとう、ポリエチレンとハムザ!

于 2013-03-23T21:38:25.090 に答える
1

AutoHotKeyはPCREを使用しているようですので、次の正規表現で処理する必要があります。

[+-]?\d+(?:\.\d+)?

于 2013-03-23T20:33:31.357 に答える