我在正则表达式方面遇到问题。我需要打印并计算以“ge”开头并以“ne”或“me”结尾的单词。当我运行代码时,仅出现以“ge”开头的单词。谁能帮助我改进我的源代码?
Pattern p = Pattern.compile("ge\\s*(\\w+)");
Matcher m = p.matcher(input);
int count=0;
List<String> outputs = new ArrayList<String>();
while (m.find()) {
count++;
outputs.add(m.group());
System.out.println(m.group());
}
System.out.println("The count is " + count);
最佳答案
\bge\w*[nm]e\b
这应该适合你。
在java中这将是
\\bge\\w*[nm]e\\b
使用\b
表示单词边界
关于java - Java 中的正则表达式 - 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33006608/