Regular expressions are very important tool for seraching in text. Below is the code snippet for executing regex search and capturing different parts of the string based on the regular expression
The groups are captured by using
(). In the regular expression above
- the first group is defined as a number with at least 1 digit
- the second group is the fixed character _
- the third group is any text.
- the fourth group is the fixed character. (we have to escape
.using \\ because in the regular expression a
.means any character or symbol or space or number).
- the fifth group is a group of characters with length > 0.
We use the
Pattern class to compile the regular expression and match the input using it to result in a
Matcher instance. This
Matcher has information about the result of the regular expression match.