2012-09-11 20 views
5

取代我在Android的字符串。我想用一些html來包裝所有4個或更多連續數字的實例。我想這將用正則表達式來完成,但我很難獲得最基本的正則表達式。的Android與正則表達式

有人可以幫助我嗎?

我想改變:

var input = "My phone is 1234567890 and my office is 7894561230"; 

var output = "My phone is <u>1234567890</u> and my office is <u>7894561230</u>"; 

回答

24

這將做到這一點:

String input = "My phone is 1234567890 and my office is 7894561230"; 
String regex = "\\d{4,}"; 
String output = input.replaceAll(regex, "<u>$0</u>"); 
System.out.println(output); 
+1

最好的,我不知道你可以使用$#用字符串。全部替換。這使得生活方式更容易。謝謝。 – raydowe

+0

@Keppil,這是什麼「\\ d {4,}」。我想用' Keppil