简体   繁体   English

Java正则表达式未捕获组

[英]Java regex not capturing group

I am applying a regex to the following string: 我将正则表达式应用于以下字符串:

2x#0$×4x#3$÷5x#0$ 2x#0 $×4x#3 $÷5x#0 $

using the following code: 使用以下代码:

String monomialRegex = "(-?\\d+(\\.\\d*)?[x][#]-?\\d+(\\.\\d*)?[\\$])";
String string2 = "2x#0$×4x#3$÷5x#0$";//the × between $ and 4 is a times symbol
string2 = string2.replaceAll(monomialRegex+"([×])"+monomialRegex+"([÷])"+monomialRegex,"÷$1%$5@×$3");

my result is coming out as: ÷2x#0$%4x#3$@× 我的结果是: ÷2x#0 $%4x#3 $ @×

From what I understand about groups: 根据我对小组的了解:

$1 = 2x#0$
$2 = ×
$3 = 4x#3$
$4 = ÷
$5 = 5x#0$

in the result, I see that instead of using group $5 it uses group $3 AND group $3 does not even appear? 结果,我看到不是使用组$ 5,而是使用组$ 3和组$ 3甚至没有出现?

Any advice as to how to handle this regex expression? 关于如何处理此正则表达式的任何建议?

Your full regex is : 您的完整正则表达式为:

(-?\d+(\.\d*)?[x][#]-?\d+(\.\d*)?[\$])([×])(-?\d+(\.\d*)?[x][#]-?\d+(\.\d*)?[\$])([÷])(-?\d+(\.\d*)?[x][#]-?\d+(\.\d*)?[\$])

Its capturing groups are : 其捕获组为:

  1. (-?\\d+(\\.\\d*)?[x][#]-?\\d+(\\.\\d*)?[\\$])
  2. (\\.\\d*)
  3. (\\.\\d*)
  4. ([×])
  5. (-?\\d+(\\.\\d*)?[x][#]-?\\d+(\\.\\d*)?[\\$])
  6. (\\.\\d*)
  7. (\\.\\d*)
  8. ([÷])
  9. (-?\\d+(\\.\\d*)?[x][#]-?\\d+(\\.\\d*)?[\\$])
  10. (\\.\\d*)
  11. (\\.\\d*)

With your current input, the following groups have content : 使用您当前的输入,以下组具有内容:

  • 1 : 2x#0$ 1:2x#0 $
  • 4 : × 4:×
  • 5 : 4x#3$ 5:4x#3 $
  • 8 : ÷ 8:÷
  • 9 : 5x#0$ 9:5x#0 $

You can visualize that on regex101 : https://regex101.com/r/Nh0xxW/1 您可以在regex101上看到它: https ://regex101.com/r/Nh0xxW/1

I would use the following to accomplish the same goal (at least if I've guessed it correctly) : 我将使用以下内容来实现相同的目标(至少,如果我猜对了):

Match (-?\d+(?:\.\d*)?x#-?\d+(?:\.\d*)?\$)×(-?\d+(?:\.\d*)?x#-?\d+(?:\.\d*)?\$)÷(-?\d+(?:\.\d*)?x#-?\d+(?:\.\d*)?\$)
Replace by %$1%$2@×$3

As seen here : https://regex101.com/r/Nh0xxW/2 如此处所示: https : //regex101.com/r/Nh0xxW/2

I mainly simplified the regex by removing useless groups (no need to capture decimals which are part of the whole monomial capturing group nor ÷ and × which you don't use in the replacement) and single-character character classes. 我主要通过删除无用的组(不需要捕获整个单项式捕获组中的小数,也不需要捕获在替换中不使用的÷和×)和单字符字符类来简化正则表达式。

You can still craft the regex with concatenation of a more simple one if you want (although I find it makes the whole thing more complex to understand in the end), and the following code will do so : 如果需要,您仍然可以使用更简单的正则表达式来制作正则表达式(尽管我发现它最终使整个事情变得更加复杂),并且以下代码将做到这一点:

String monomialRegex = "(-?\\d+(?:\\.\\d*)?x#-?\\d+(?:\\.\\d*)?\\$)";
String completeRegex = monomialRegex + "×" + monomialRegex + "÷" + monomialRegex;

I've tested the java code on ideone : https://ideone.com/Eg8Zz6 我已经在ideone上测试过Java代码: https ://ideone.com/Eg8Zz6

The problem was in the sub-groups inside monomialRegex: 问题出在monomialRegex内部的子组中:

I got rid of the subgroup: (\\.\\d*)? 我摆脱了这个小组:(\\。\\ d *)? within monomialRegex. 在monomialRegex中。

I replaced this subgroup with [\\.]?[\\d+]? 我用[\\。]?[\\ d +]替换了此子组? and it works. 而且有效。 I did not need to change the ordering of the groups within the entire regex. 我不需要在整个正则表达式中更改组的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM