Home > Java > javaTutorial > body text

How to match bold fields in HTML script using regular expression in Java?

WBOY
Release: 2023-08-27 18:25:06
forward
1138 people have browsed it

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>"
Copy after login

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.

Example

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());
      }
   }
}
Copy after login

Output

<b>is</b>
<b>example</b>
<b>script</b>
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!