<span></span> 正则表达式 java 100
正则表达式该怎么写?提取738626 展开
假设得到的授权码存于str中
方法一:
直接正则表达式提取授权码
Pattern pattern = Pattern.compile("\d+");
Matcher matcher = pattern.matcher(str);
String string = matcher.group(1);
方法二:
去除字符串中多余的html标签
Pattern pattern = Pattern.compile("<.+?>", Pattern.DOTALL);
Matcher matcher = pattern.matcher(str);
String string = matcher.replaceAll("");
方法一
String ResultString = null;
try {
Pattern regex = Pattern.compile("<span class=\"fb\">(.*)</span>");
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
ResultString = regexMatcher.group();
}
} catch (PatternSyntaxException ex) {
// Syntax error in the regular expression
}
如果上述ID不唯一可用以下方式
List<String> matchList = new ArrayList<String>();
try {
Pattern regex = Pattern.compile("<span class=\"fb\">(.*)</span>");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
matchList.add(regexMatcher.group());
}
} catch (PatternSyntaxException ex) {
// Syntax error in the regular expression
}
String s = "<span class="fb">738626</span> </div>";
Pattern pat = Pattern.compile(regEx);
Matcher mat = pat.matcher(s);
for(int i=1;i<=mat.groupCount();i++){
System.out.println(mat.group(i));
}