2014-04-08 24 views

回答

3

不要使用正則表達式這個任務,使用String#split(即需要正則表達式):

String splitted = myStr.split("\\."); 
String myStr = splitted[1]; 

注意,它的建議,以檢查是否字符串實際上包含了「」在你分裂之前。

+0

如果輸入中沒有點,將會失敗,並輸入AIOOB – fge

+1

@fge這就是爲什麼我建議在分割前檢查。 – Maroun

+1

好吧,只需檢查分割陣列的長度...不需要檢查點 – fge

3

如果你堅持使用正則表達式,

做到這一點:

\.(.+) 

第一組將包含後.

+0

@thefourtheye,更新 –

+0

該組將包含**第一個**點之後的所有內容。 – Toto

+0

@ M42,是的。 OP想要對嗎? –

5

使用捕捉組串,你的正則表達式是:

\\.([^.]*)$ 
+0

+1用於實際顯示* regex *的解決方案。 – Maroun

+0

我想你應該刪除'[^。]'因爲OP要匹配任何字符 –

+0

@AmitJoki嗯......你能否詳細說明一下? – devnull

0

這可以做到:

private static final Pattern AFTER_DOT = Pattern.compile("[^.]+\\.([^.]+)"); 

// 
final Matcher m = AFTER_DOT.matcher(input); 
return m.matches() ? m.group(1) : null; 

假設輸入中只有一個點!

相關問題