如何使用Java中的正则表达式在HTML脚本中匹配粗体字段?

如何使用Java中的正则表达式在HTML脚本中匹配粗体字段?

正则表达式"S"匹配一个非空白字符,下面的正则表达式匹配粗体标记之间的一个或多个非空格字符。

"(S+)"

登录后复制

因此,要匹配 HTML 脚本中的粗体字段,您需要 -

  • 使用compile() 方法编译上述正则表达式。

  • 使用 matcher() 方法从获取的模式中检索匹配器。

  • 使用组打印输入字符串的匹配部分() 方法。

示例

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

登录后复制

以上就是如何使用Java中的正则表达式在HTML脚本中匹配粗体字段?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!