
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to match bold fields in a HTML script using a regular expression in Java?
The regular expression "\S" matches a non-whitespace character and the following regular expression matches one or more non space characters between the bold tags.
"(\S+)"
Therefore to match the bold fields in a HTML script you need to −
Compile the above regular expression using the compile() method.
Retrieve the matcher from the obtained pattern using the matcher() method.
Print the matched parts 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()); } } }
Output
<b>is</b> <b>example</b> <b>script</b>
- Related Articles
- Program to match vowels in a string using regular expression in Java
- How to match a word in python using Regular Expression?
- How to match a whitespace in python using Regular Expression?
- How to match digits using Java Regular Expression (RegEx)
- How to match a nonwhitespace character in python using Regular Expression?
- How to match a single character in python using Regular Expression?
- How to match non-digits using Java Regular Expression (RegEx)
- How to use regular expression in Java to pattern match?
- How to match only digits in Python using Regular Expression?
- How to match a regular expression against a string?
- How to match parentheses in Python regular expression?
- PHP – Match regular expression using mb_ereg_match()
- How to match any one lowercase vowel in python using Regular Expression?
- How to write a regular expression to match either a or b in Python?
- MongoDB regular expression to match a specific record?

Advertisements