Java正则表达式模式&;匹配器

Java正则表达式模式&;匹配器,java,regex,matcher,Java,Regex,Matcher,我尝试使用数学工具解析这个字符串:“2+30*4+(5+6)*7” 使用此模式:“\d*|[()+*-]” 出于某种原因,匹配器会正确拆分字符串,但在检查拆分后的字符串时,它不会正确拆分字符串,只留下空字符串作为数字: String s = "2+30*4+(5+6)*7"; Pattern p = Pattern.compile("\\d*|[()+*-]"); Matcher m = p.matcher(s); while (m.find()) {

我尝试使用数学工具解析这个字符串:“2+30*4+(5+6)*7”

使用此模式:“\d*|[()+*-]”

出于某种原因,匹配器会正确拆分字符串,但在检查拆分后的字符串时,它不会正确拆分字符串,只留下空字符串作为数字:

String s = "2+30*4+(5+6)*7";        
    Pattern p = Pattern.compile("\\d*|[()+*-]");
    Matcher m = p.matcher(s);
    while (m.find()) {
          System.out.print("Start index: " + m.start());
          System.out.print(" End index: " + m.end() + " ");
          System.out.println("-----> " + m.group());
    }
这将提供以下输出:

Start index: 0 End index: 1 -----> 2
Start index: 1 End index: 1 -----> 
Start index: 2 End index: 4 -----> 30
Start index: 4 End index: 4 -----> 
Start index: 5 End index: 6 -----> 4
Start index: 6 End index: 6 -----> 
Start index: 7 End index: 7 -----> 
Start index: 8 End index: 9 -----> 5
Start index: 9 End index: 9 -----> 
Start index: 10 End index: 11 -----> 6
Start index: 11 End index: 11 -----> 
Start index: 12 End index: 12 -----> 
Start index: 13 End index: 14 -----> 7
Start index: 14 End index: 14 -----> 
我不明白为什么,例如,在第二行中,结束索引是1(而不是2),结果是一个空字符串: 开始索引:1结束索引:1---------->


顺便说一句,当我将模式的顺序更改为“[()+-]|\d”时,效果很好。
\\d*
允许使用空字符串,因为它表示零位或更多位。如果不想查找零位(空)字符串,请将
\\d*
更改为
\\d+

演示

输出:

Start index: 0 End index: 1 -----> 2
Start index: 1 End index: 2 -----> +
Start index: 2 End index: 4 -----> 30
Start index: 4 End index: 5 -----> *
Start index: 5 End index: 6 -----> 4
Start index: 6 End index: 7 -----> +
Start index: 7 End index: 8 -----> (
Start index: 8 End index: 9 -----> 5
Start index: 9 End index: 10 -----> +
Start index: 10 End index: 11 -----> 6
Start index: 11 End index: 12 -----> )

如果您对您的代币的位置不感兴趣,您也可以在
+
-
*
/
之前或之后拆分
,如

String s = "2+30*4+(5+6)*7";
String[] tokens = s.split("(?<=[+\\-*/()])|(?=[+\\-*/()])");
for (String token : tokens)
    System.out.println(token);

\\d*
允许使用空字符串,因为它表示零位或更多位。如果不想查找零位(空)字符串,请将
\\d*
更改为
\\d+

演示

输出:

Start index: 0 End index: 1 -----> 2
Start index: 1 End index: 2 -----> +
Start index: 2 End index: 4 -----> 30
Start index: 4 End index: 5 -----> *
Start index: 5 End index: 6 -----> 4
Start index: 6 End index: 7 -----> +
Start index: 7 End index: 8 -----> (
Start index: 8 End index: 9 -----> 5
Start index: 9 End index: 10 -----> +
Start index: 10 End index: 11 -----> 6
Start index: 11 End index: 12 -----> )

如果您对您的代币的位置不感兴趣,您也可以在
+
-
*
/
之前或之后拆分
,如

String s = "2+30*4+(5+6)*7";
String[] tokens = s.split("(?<=[+\\-*/()])|(?=[+\\-*/()])");
for (String token : tokens)
    System.out.println(token);

\\d*
匹配零位或多位数字。因此,在第一次匹配之后,匹配器会查看
“+30*4+(5+6)*7”
,匹配器会问的第一件事是,“这个字符串是否以零或更多数字开头?天哪,是的!”(它会先检查这个,因为
\\d*
在模式中首先出现。)这就是匹配器返回空字符串的原因(由零位组成的字符串)


将其更改为匹配一个或多个数字的
\\d+
,应该可以工作。

\\d*
匹配零个或多个数字。因此,在第一次匹配后,匹配者会查看
“+30*4+(5+6)*7”
,匹配者首先会问,“这个字符串是否以零个或多个数字开头?天哪,是的!”(它首先检查这个,因为
\\d*
首先出现在模式中。)这就是匹配器返回空字符串(零位字符串)的原因


将其更改为与一个或多个数字匹配的
\\d+
,应该可以使用。

您使用regix
\\d*.[()+*-]
尝试的内容可以表示为

它匹配零个或多个数字

您需要使用regix
\\d+|[()+*-]
将其更改为一个或多个,并可以表示为


您使用regix尝试的内容
\\d*.[()+*-]
可以表示为

它匹配零个或多个数字

您需要使用regix
\\d+|[()+*-]
将其更改为一个或多个,并可以表示为