表达式使用的特殊构造 [英] Special construct of expression use
问题描述
使用If-Then-Else Conditionals
a> 在正则表达式中,我想知道尝试将 many 构造操纵成多个匹配的单个表达式的可能结果.
With the construct of using If-Then-Else Conditionals
in regular expressions, I would like to know the possible outcome of trying to manipulate many constructs into a single expression for multiple matches.
让我们以下面这个例子为例.
Let's take this example below.
foo(bar)?(?(1)baz|quz)
现在与一个匹配前一个条件的表达式组合,然后我们用以下条件添加到前一个条件..
Now being combined with an expression, which matches the previous conditions and then we add on to the previous with the following conditions..
foo(bar)?(?(1)baz|quz)|(?(?=.*baz)bar|foo)
我主要是问您是否应该以这种方式构建正则表达式,以及您需要以这种方式使用它的目的是什么?
Mainly I am asking should you construct a regular expression in this way, and what would ever be the purpose that you would need to use it in this way?
推荐答案
你应该用这种方式构造一个正则表达式吗?曾经是您需要以这种方式使用它的目的吗?
should you construct a regular expression in this way, and what would ever be the purpose that you would need to use it in this way?
在这种情况下,并且可能在大多数情况下,我会说不".
In this case, and probably most cases, I would say "no".
我经常发现条件可以重写为环视或在交替中简化.
I often find that conditionals can be rewritten as lookarounds or simplified within alternations.
例如,在我看来,你提供的正则表达式,
For instance, it seems to me that the regex you supplied,
foo(bar)?(?(1)baz|quz)|(?(?=.*baz)bar|foo)
可以替换为更清晰的
bar(?=.*baz)|foo(?:quz|barbaz)?
这给了我们两个简单的匹配路径.
which gives us two simple match paths.
但是您发布问题已经六个月了.在这段时间里,回答了很多关于 SO 的问题,你有没有觉得需要这样的建设?
But it's been six months since you posted the question. During this time, answering a great many questions on SO, have you ever felt the need for that kind of construction?
这篇关于表达式使用的特殊构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!