[英]Regex in Java - Regular Expression
我在正則表達式中遇到問題。 我需要打印並計算以“ 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
表示word boundary
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.