簡體   English   中英

Java中的正則表達式-正則表達式

[英]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

好吧,你有點想不到正則表達式的后半部分

ge\s*(\w+)[nm]e

這將起作用。

正則表達式101

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM