本文介绍了关于查找数字字符串的 Java 正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上学习 Java 正则表达式教程时,对一个小程序感到困惑.

I was learning a Java regular expression tutorial online and got confused about one small program.

  // String to be scanned to find the pattern.
  String line = "This order was places for QT3000! OK?";
  String pattern = "(.*)(\\d+)(.*)";

  // Create a Pattern object
  Pattern r = Pattern.compile(pattern);

  // Now create matcher object.
  Matcher m = r.matcher(line);
  if (m.find( )) {
     System.out.println("Found value: " + m.group(0) );
     System.out.println("Found value: " + m.group(1) );
     System.out.println("Found value: " + m.group(2) );
  }

打印出来的结果是:

Found value: This order was places for QT3000! OK?

Found value: This order was places for QT300

Found value: 0

我不知道为什么 group(1) 会得到上述值?为什么它在'QT3000'的最后一个零之前停止?

I have no idea why the group(1) gets value the above value? Why it stops before the last zero of 'QT3000'?

非常感谢!

推荐答案

第一组 (.*)(这是索引 1 - 索引 0 是整体正则表达式)是贪婪匹配.它尽可能多地捕捉,同时让整体表达仍然匹配.因此它可以占用字符串中的第二个 0,只剩下 0 来匹配 (\\d+).如果您想要不同的行为,那么您应该阅读贪婪和非贪婪匹配,或者找到更合适的模式.

The first group of (.*) (this is index 1 - index 0 is the overall regular expression) is a greedy match. It captures as much as it can while letting the overall expression still match. Thus it can take up to the second 0 in the string, leaving just 0 to match (\\d+). If you want different behaviour, then you should read up on greedy and non-greedy matches, or find a more appropriate pattern.

这篇关于关于查找数字字符串的 Java 正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 12:12