Java 要适应无序列表的Regexp
我有很多字符串,其中存储了如下值:Java 要适应无序列表的Regexp,java,regex,Java,Regex,我有很多字符串,其中存储了如下值: (40|21|33)(111|222|333)(1|2) 正如您在上面看到的,有三个组,每个组由一个或多个恒定长度的值组成,但给定组中的值可以以任何顺序出现(如果是升序或降序,则未定义) 我正在使用正则表达式查看一组值,我坚持使用一种方法来检测给定组中是否存在一组值(即20或31)(即(40 | 21 | 33)) 我已经制作了正则表达式,它可以检测是否有任何值,即20或31,但它应该只在所有指定值时适用: (\()([0-9]{2}(\|)?)*((20(
(40|21|33)(111|222|333)(1|2)
正如您在上面看到的,有三个组,每个组由一个或多个恒定长度的值组成,但给定组中的值可以以任何顺序出现(如果是升序或降序,则未定义)
我正在使用正则表达式查看一组值,我坚持使用一种方法来检测给定组中是否存在一组值(即20或31
)(即(40 | 21 | 33)
)
我已经制作了正则表达式,它可以检测是否有任何值,即20或31
,但它应该只在所有指定值时适用:
(\()([0-9]{2}(\|)?)*((20(\|)?)|(31(\|)?))([0-9]{2}(\|)?)*(\))
假设组中的值顺序未知,是否有方法检测给定组中是否所有给定值的
我只是想澄清一下
您可以通过以下方式执行此操作:
或者,在Java中:
Pattern regex = Pattern.compile(
"\\( # Match (\n" +
"(?=[^()]*\\b21\\b) # Assert that 21 can be matched within this group\n" +
"(?=[^()]*\\b33\\b) # Assert that 33 can be matched within this group\n" +
"\\d+ # Match a number \n" +
"(?: # Start of non-capturing group:\n" +
" \\| # Match |\n" +
" \\d+ # Match a number\n" +
")* # any number of times, including 0\n" +
"\\) # Match )",
Pattern.COMMENTS);
看吧。要么你不太清楚,要么我太笨了,无法理解你的意思question@aelor做了一点编辑,也许这可以帮助谢谢你,这正是我要找的。
\( # Match (
(?=[^()]*\b21\b) # Assert that 21 can be matched within this group
(?=[^()]*\b33\b) # Assert that 33 can be matched within this group
\d+ # Match a number
(?: # Start of non-capturing group:
\| # Match |
\d+ # Match a number
)* # any number of times, including 0
\) # Match )
Pattern regex = Pattern.compile(
"\\( # Match (\n" +
"(?=[^()]*\\b21\\b) # Assert that 21 can be matched within this group\n" +
"(?=[^()]*\\b33\\b) # Assert that 33 can be matched within this group\n" +
"\\d+ # Match a number \n" +
"(?: # Start of non-capturing group:\n" +
" \\| # Match |\n" +
" \\d+ # Match a number\n" +
")* # any number of times, including 0\n" +
"\\) # Match )",
Pattern.COMMENTS);