如何合并正则表达式组匹配? [英] How to merge regex group matches?

查看:52
本文介绍了如何合并正则表达式组匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下一行:

one two three

是否可以编写一个会在下面返回的正则表达式?

Is it possible to write a regex that would return below?

one three

我当然可以将每个部分放在一个单独的组中,但是否可以在单场比赛中捕获它?

I can of course get each part in a separate group but is it possible to capture that in a single match?

推荐答案

简单地说:不,它无法完成(如对您原始问题的评论中所述).

To put it simply: no, it can't be done (as discussed in comments on your original question).

要找出原因,让我们更一般地看一下.正则表达式可以建模为(通常很复杂)确定性有限自动机,也称为 DFA,而您的普通正则表达式引擎是作为一个引擎实现的.这意味着正则表达式将一次包含零个或一个字符,并查看它是否与当前标记匹配.如果不是,它将回溯并尝试匹配当前阶段的任何可能的标记(通过交替操作 | 完成).如果不能,它停止并报告它不能匹配.由于 DFA 按顺序对输入进行操作,因此根据定义,您所要求的基本上是不可能的.

To find out why, let's look at it a bit more generally. A regular expression can be modelled as a (often complex) deterministic finite automaton, also known as a DFA, and your average regex engine is implemented as one. What this means is that the regex will slurp zero or one character at a time, and see if it matches the current token. If not, it will backtrack and attempt to match any possible token at the current stage (done with the alternation operation |). If unable, it halts and reports it cannot match. Since a DFA operates on the input in sequential order, what you're asking for is basically impossible by definition.

这篇关于如何合并正则表达式组匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆