
正则表达式"\S"匹配一个非空白字符,下面的正则表达式匹配粗体标记之间的一个或多个非空格字符。
"(\S+)"
因此,要匹配 HTML 脚本中的粗体字段,您需要 -
使用compile() 方法编译上述正则表达式。
使用 matcher() 方法从获取的模式中检索匹配器。
-
使用组打印输入字符串的匹配部分() 方法。
立即学习“Java免费学习笔记(深入)”;
示例
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
String str = "This is an example>/b> HTML script.
";
//Regular expression to match contents of the bold tags
String regex = "(\S+)";
//Creating a pattern object
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(str);
//Creating an empty string buffer
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}输出
is example script











