本文介绍了正则表达式java。为何使用十字路口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从这个关于java正则表达式的oracle教程中获取,以下位:

I have taken from this oracle tutorial on java regex, the following bit:

创建单个字符类匹配只有所有嵌套类的共同字符
,使用&&和[0-9&& [345]]。这个
特殊交叉点创建一个单独的字符类,仅匹配
两个字符类共有的数字:3,4和5。

To create a single character class matching only the characters common to all of its nested classes, use &&, as in [0-9&&[345]]. This particular intersection creates a single character class matching only the numbers common to both character classes: 3, 4, and 5.

输入您的正则表达式:[0-9&& [345]]输入要搜索的输入字符串:3 I
发现文本3从索引0开始到索引1结束。

Enter your regex: [0-9&&[345]] Enter input string to search: 3 I found the text "3" starting at index 0 and ending at index 1.

为什么它会有用?我的意思是,如果只想模仿 345 为什么不仅仅是[ 345 ]而不是交叉点?

Why would it be useful? I mean if one wants to pattern only 345 why not only [345] instead of "the intersection"?

提前致谢。

推荐答案

让我们考虑一个简单的问题:在字符串中匹配英语辅音。列出所有辅音(或范围列表)将是一种方式:

Let us consider a simple problem: match English consonants in a string. Listing out all consonants (or a list of ranges) would be one way:

[B-DF-HJ-NP-TV-Zb-df-hj-np-tv-z]

另一种方法是使用环视:

Another way is to use look-around:

(?=[A-Za-z])[^AEIOUaeiou]
(?![AEIOUaeiou])[A-Za-z]

不确定是否有其他方法可以在没有使用字符类交集。

Not sure if there is any other way to do this without the use of character class intersection.

字符类交集解决方案(Java):

Character class intersection solution (Java):

[A-Za-z&&[^AEIOUaeiou]]

对于.NET,没有交集,但有字符类减法:

For .NET, there is no intersection, but there is character class subtraction:

[A-Za-z-[AEIOUaeiou]]

我不知道实现细节,但如果字符类交集/减法更快,我不会感到惊讶而不是使用look-around,如果字符类操作不可用,这是最干净的选择。

I don't know the implementation details, but I wouldn't be surprised if character class intersection/subtraction is faster than the use of look-around, which is the cleanest alternative if character class operation is not available.

另一个可能的我们年龄是指您有预先构建的字符类,并且想要从中删除一些字符。我遇到过一个可能适用于类交集的情况是匹配除新行之外的所有空白字符。

Another possible usage is when you have a pre-built character class and you want to remove some characters from it. One case that I have come across where class intersection might be applicable would be to match all whitespace characters, except for new line.

另一个可能的用例@beerbajay已评论过:

Another possible use case as @beerbajay has commented:

这篇关于正则表达式java。为何使用十字路口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!