AUTOHOTKEY: RegExMatch() 一系列数字和字母
AUTOHOTKEY: RegExMatch() a series of numbers and letters
我已经在 http://www.regextester.com/
中测试了我的正则表达式
([0-9]{4,4})([A-Z]{2})([0-9]{1,3})
和我想要的下面的字符串完美匹配
1234AB123
2000AZ20
1000XY753
但是当我在 Autohotkey 中尝试时,我得到了 0 个结果
test := RegExMatch("2000SY155","([0-9]{4,4})([A-Z]{2})([0-9]{1,3})")
MsgBox %test%
测试:
- 前 4 个字符必须是数字
- 接下来的2个字符必须是大写字母
- 接下来的 1 到 3 个字符必须是数字
你有很多 ( )
这是正确的实现:
test := RegExMatch("1234AB123","[0-9]{4,4}([A-Z]{2})[0-9]{1,3}")
编辑:
所以我注意到您希望此模式匹配,但您并没有真正说明。
这是我能够提出的与您的要求相匹配的方法,它可能不是最好的解决方案,但它有效:
test := RegExMatch("1234AB567","^[0-9]{4,4}[A-Z]{2}(?![0-9]{4,})[0-9$]{1,3}")
分解:
RegExMatch(Haystack, NeedleRegEx [, UnquotedOutputVar = "", StartingPosition = 1])
Circumflex (^) and dollar sign ($) are called anchors because
they don't consume any characters; instead, they tie the pattern to
the beginning or end of the string being searched.
^ may appear at the beginning of a pattern to require the match to occur at
the very beginning of a line. For example, **
** matches abc123 but not 123abc.
$ may appear at the end of a pattern to require the match to occur at the very > end of a line. For example, abc$ matches 123abc but not abc123.
因此,通过添加 Circumflex,我们要求我们的模式 [0-9]{4,4}
位于 Haystack 的开头。
Look-ahead and look-behind assertions: The groups (?=...), (?!...) are
called assertions because they demand a condition to be met but don't
consume any characters.
(?!...) is a negative look-ahead because it requires that the specified pattern not exist.
我们的下一个模式正在寻找后面没有四个或更多数字字符的两个大写字母字符 [A-Z]{2}(?![0-9]{4,})
。
最后我们的最后一个模式需要匹配一到三个数字字符作为我们 Haystack 中的最后一个字符 [0-9$]{1,3}
test := RegExMatch("2000SY155","([0-9]{4,4})([A-Z]{2})([0-9]{1,3})")
MsgBox %test%
But when I try it in Autohotkey I get 0 result
消息框对我来说是正确的 returns 1
,这意味着您的初始脚本与我的版本兼容。通常,大括号在正则表达式中没有问题,你可以放多少放多少...也许你的 AutoHotkey 版本过时了?
我已经在 http://www.regextester.com/
中测试了我的正则表达式([0-9]{4,4})([A-Z]{2})([0-9]{1,3})
和我想要的下面的字符串完美匹配
1234AB123
2000AZ20
1000XY753
但是当我在 Autohotkey 中尝试时,我得到了 0 个结果
test := RegExMatch("2000SY155","([0-9]{4,4})([A-Z]{2})([0-9]{1,3})")
MsgBox %test%
测试:
- 前 4 个字符必须是数字
- 接下来的2个字符必须是大写字母
- 接下来的 1 到 3 个字符必须是数字
你有很多 ( )
这是正确的实现:
test := RegExMatch("1234AB123","[0-9]{4,4}([A-Z]{2})[0-9]{1,3}")
编辑:
所以我注意到您希望此模式匹配,但您并没有真正说明。
这是我能够提出的与您的要求相匹配的方法,它可能不是最好的解决方案,但它有效:
test := RegExMatch("1234AB567","^[0-9]{4,4}[A-Z]{2}(?![0-9]{4,})[0-9$]{1,3}")
分解:
RegExMatch(Haystack, NeedleRegEx [, UnquotedOutputVar = "", StartingPosition = 1])
Circumflex (^) and dollar sign ($) are called anchors because they don't consume any characters; instead, they tie the pattern to the beginning or end of the string being searched.
^ may appear at the beginning of a pattern to require the match to occur at the very beginning of a line. For example, ** ** matches abc123 but not 123abc.
$ may appear at the end of a pattern to require the match to occur at the very > end of a line. For example, abc$ matches 123abc but not abc123.
因此,通过添加 Circumflex,我们要求我们的模式 [0-9]{4,4}
位于 Haystack 的开头。
Look-ahead and look-behind assertions: The groups (?=...), (?!...) are called assertions because they demand a condition to be met but don't consume any characters.
(?!...) is a negative look-ahead because it requires that the specified pattern not exist.
我们的下一个模式正在寻找后面没有四个或更多数字字符的两个大写字母字符 [A-Z]{2}(?![0-9]{4,})
。
最后我们的最后一个模式需要匹配一到三个数字字符作为我们 Haystack 中的最后一个字符 [0-9$]{1,3}
test := RegExMatch("2000SY155","([0-9]{4,4})([A-Z]{2})([0-9]{1,3})")
MsgBox %test%
But when I try it in Autohotkey I get 0 result
消息框对我来说是正确的 returns 1
,这意味着您的初始脚本与我的版本兼容。通常,大括号在正则表达式中没有问题,你可以放多少放多少...也许你的 AutoHotkey 版本过时了?