【发布时间】:2010-10-04 02:19:52
【问题描述】:
我想我的问题最好用一个(简化的)例子来解释。
正则表达式 1:
^\d+_[a-z]+$
正则表达式 2:
^\d*$
Regex 1 将从不匹配 regex 2 匹配的字符串。 因此,假设正则表达式 1 与正则表达式 2正交。
正如许多人问我所说的正交是什么意思,我会试着澄清一下:
让 S1 是正则表达式 1 匹配的(无限)字符串集。 S2 是正则表达式 2 匹配的字符串集。 正则表达式 2 与正则表达式 1 正交 iff S1 和 S2 的交集为空。 正则表达式 ^\d_a$ 将 不正交,因为字符串 '2_a' 在集合 S1 和 S2 中。
如果两个正则表达式相互正交,如何以编程方式确定?
最好的情况是一些库实现了这样的方法:
/**
* @return True if the regex is orthogonal (i.e. "intersection is empty"), False otherwise or Null if it can't be determined
*/
public Boolean isRegexOrthogonal(Pattern regex1, Pattern regex2);
【问题讨论】: