Home >Java >javaTutorial >How to match bold fields in HTML script using regular expression in Java?
Regular Expression"\S" Matches a non-whitespace character, the following regular expression matches one or more non-space characters between bold tags .
"<strong>(\S+)</strong>"
So, to match bold fields in an HTML script, you need to -
Compile the above regular expression using the compile() method.
Use the matcher() method to retrieve a matcher from the obtained pattern.
Print the matching portion of the input string using the group() method.
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String[] args) { String str = "<p>This <b>is</b> an <b>example>/b> HTML <b>script</b>.</p>"; //Regular expression to match contents of the bold tags String regex = "<b>(\S+)</b>"; //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()); } } }
<b>is</b> <b>example</b> <b>script</b>
The above is the detailed content of How to match bold fields in HTML script using regular expression in Java?. For more information, please follow other related articles on the PHP Chinese website!