如何从前瞻转换正则表达式 [英] How to convert regexp from lookahead
问题描述
我在PHP和其他出色的代码中拥有以下正则表达式.
I have the following Regex in PHP and other code that is doing a great job.
/^(?:(?=[^ ]+\d)(?:[A-Z0-9]+))|(?:[A-Z0-9]+) +?(?=.*\d)(?:[A-Z0-9]+)?
事实证明,Go不支持headheads,我对如何将其转换一无所知.
It turns out that Go doesn't support lookheads and I'm at a loss on how to convert it across.
Debuggex链接中包含一些我用来验证代码的测试数据.
The Debuggex link has some test data I was using to qualify the code before.
推荐答案
首先,交替/^...
的左侧不匹配任何内容,因为/
永远不会在输入开始之前出现.
Firstly, the left hand side of the alternation /^...
matches nothing, because /
can never appear before start of input.
仅留下(?:[A-Z0-9]+) +?(?=.*\d)
,可以表示为:
((?:[A-Z0-9]+) ).*\d.*
所使用的术语" +?"
实际上与" "
相同.
The term " +?"
as used is identical in effect to just " "
.
但是请注意,这消耗了比原始输入更多的输入,这是不可避免的.您原来的比赛现在在第1组中.
Note however that this consumes more input than the original, which is unavoidable. Your original match is now in group 1.
这篇关于如何从前瞻转换正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!