所以,如果我想與給定值來代替所有的數字,我可以只使用Java的正則表達式參數
"hello8".replaceAll("[1-9]", "!");
你好!
現在有辦法獲得實際匹配的數字並將其添加到字符串?
例如!
你好8
所以,如果我想與給定值來代替所有的數字,我可以只使用Java的正則表達式參數
"hello8".replaceAll("[1-9]", "!");
你好!
現在有辦法獲得實際匹配的數字並將其添加到字符串?
例如!
你好8
一種選擇是設置一個捕獲組:
"hello8".replaceAll("([1-9])", "!$1");
另一種選擇是使用$0
,這意味着整場比賽:
"hello8".replaceAll("[1-9]", "!$0");
在這裏你去!
String s = "hello8";
String y = null;
String t = null;
Pattern p = Pattern.compile("[1-9]");
Matcher m = p.matcher(s);
while(m.find()) {
y = (m.group());
t = "!"+y;
s = s.replace(y.toString(), t.toString());
}
System.out.println(s);
得等5分鐘,除非一個完美的答案... – 2010-12-17 22:16:34