java 正則表達式提取字符串

java 正則表達式提取字符串

有時需要提取一個字符串中的一部分內容,必須要用到分組。


String splitReqString = "uid=923933544/sid=DFS32DSFS";
String uid = getMatcher("uid=([\\d]+)", splitReqString);
String sid = getMatcher("sid=([0-9a-zA-Z]+)", splitReqString);

public static String getMatcher(String regex, String source) {
String result = "";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(source);
while (matcher.find()) {
result = matcher.group(1);//只取第一組
}
return result;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章