当前位置:文档之家› 【IT专家】在正则表达式中使用OR运算符

【IT专家】在正则表达式中使用OR运算符

本文由我司收集整编,推荐下载,如有疑问,请与我司联系
在正则表达式中使用OR 运算符
How can I use OR in a Java regex? I tried the following, but it’s returning null instead of the text.
如何在Java 正则表达式中使用OR?我尝试了以下,但它返回null 而不是文本。

Pattern reg = Patternpile(“\\*+|#+ (.+?)”);Matcher matcher = reg.matcher(“*kdkdk”); \\ “#aksdasd”matcher.find();System.out.println(matcher.group(1)); 3
The regex syntax for searching for X or Y is (X|Y). The parentheses are required if you have anything else in the pattern. You were searching for one of these patterns:
用于搜索X 或Y 的正则表达式语法是(X | Y)。

如果模式中还有其他任何内容,
则必须使用括号。

您正在搜索以下模式之一:
a literal * repeated one or more times
文字*重复一次或多次
OR
要么
a literal # repeated one or more times, followed by a space, followed by one or more
of any character, matching a minimum number of times
文字#重复一次或多次,后跟一个空格,后跟一个或多个任何字符,匹配最少次

This pattern matches * using the first part of the OR, but since that subpattern defines
no capture groups, matcher.group(1) will be null. If you printed matcher.group(0), you would get * as the output.
此模式使用OR 的第一部分匹配*,但由于该子模式不定义捕获组,因此
matcher.group(1)将为null。

如果你打印matcher.group(0),你会得到*作为输
出。

If you want to capture the first character to the right of a space on a line that starts with
either “*”or “#”repeated some number of times, followed by a space and at least one。

相关主题