java - 理解 Java 正则表达式

标签 java regex

Enter your regex: a?
Enter input string to search: ababaaaab
I found the text "a" starting at index 0 and ending at index 1.
I found the text "" starting at index 1 and ending at index 1.
I found the text "a" starting at index 2 and ending at index 3.
I found the text "" starting at index 3 and ending at index 3.
I found the text "a" starting at index 4 and ending at index 5.
I found the text "a" starting at index 5 and ending at index 6.
I found the text "a" starting at index 6 and ending at index 7.
I found the text "a" starting at index 7 and ending at index 8.
I found the text "" starting at index 8 and ending at index 8.
I found the text "" starting at index 9 and ending at index 9.

Enter your regex: a*
Enter input string to search: ababaaaab
I found the text "a" starting at index 0 and ending at index 1.
I found the text "" starting at index 1 and ending at index 1.
I found the text "a" starting at index 2 and ending at index 3.
I found the text "" starting at index 3 and ending at index 3.
I found the text "aaaa" starting at index 4 and ending at index 8.
I found the text "" starting at index 8 and ending at index 8.
I found the text "" starting at index 9 and ending at index 9.

Enter your regex: a+
Enter input string to search: ababaaaab
I found the text "a" starting at index 0 and ending at index 1.
I found the text "a" starting at index 2 and ending at index 3.
I found the text "aaaa" starting at index 4 and ending at index 8.  



上面的示例来自有关量词的 Java 文档
根据我对上面给出的示例的理解,以下是贪婪量词的工作原理:

?和 *

将查找所提到的字符是否存在。如果找不到它们前面的字符,则会将该位置标记为零长度匹配。
* 用于逐个字符匹配,而 ? 用于一组。

+

+ 将进行正则表达式的逐组匹配。它不会进行零长度匹配。

我的问题是:
我对量词的理解是正确的还是我弄乱了一些东西?
另外,是否有关于 java regex 的大脑友好型指南(Google 上没有针对初学者的指南)?

最佳答案

  • goes for a character by character match while ? goes for a group.

* 匹配零个或多个。 ? 匹配零或 1。

这些都与群体或角色无关。正则表达式中的“组”或“捕获组”是一个带括号的元素,可以通过 java.util.regex.Matcher.group 方法单独提取。

(foo)* 匹配 """foo""foofoo" 所以显然主体不必是单个字符。

(foo)? 匹配 """foo" 但不匹配 "foofoo"

  • will go for group by group matching of the regex. It does not make zero-length matches.

没有。 + 与正文匹配一次或多次,因此 x* 相当于 (?:x+)?+ 运算符可以很好地执行零长度匹配。 ()+ 匹配空字符串。

关于java - 理解 Java 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14928316/

相关文章:

java - For循环导致JFrame(Eclipse)黑屏

java - Struts2 推送通知/WebSocket 交互最佳实践

Java字符串分割正则表达式返回带有多个分隔符的空字符串

匹配输入中的 Java 正则表达式

php - 带 float /小数的字母数字

java - 二维数组和空指针异常 (Java)

java - 将 XMLGregorianCalendar 转换为 Oracle 时间戳值

java - 有什么不同

javascript - Javascript 中两个不同字符串正则表达式之间的所有文本

正则表达式删除序数