A regular expression, comprised of characters, serves as a pattern for searching within text data, allowing you to specify the criteria for your search.
A regular expression can range from a single character to a more intricate pattern.
Regular expressions enable various types of text search and replace operations.
In Java, there isn’t a dedicated Regular Expression class, but we can utilize the java.util.regex package to handle regular expressions, which includes several classes.
Determine if the word “code7school” appears in a sentence.
import
// Outputs Match found |
In this instance, the sentence is being scanned for the presence of the term “code7school”.
Initially, the pattern is established using the Pattern.compile() method. The first argument specifies the pattern sought, while the second argument, an optional flag, signifies a case-insensitive search.
The matcher() method is employed to seek the pattern within a string. It yields a Matcher object that encapsulates details regarding the executed search.
The find() method yields true if the pattern is discovered within the string, and false if it is not.
Flags passed into the compile() method modify the behavior of the search operation. Here are some examples:
Brackets in the Pattern.compile() method’s first parameter denote a pattern, specifying what is sought during a search.
They are utilized to identify a range of characters.
Expression |
Description |
[abc] |
Locate a single character from the choices listed within the brackets. |
[^abc] |
Locate a single character that is outside the range specified within the brackets. |
[0-9] |
Locate a single character within the numeric range from 0 to 9. |
Metacharacters are characters endowed with special significance.
Metacharacter |
Description |
| |
Locate a match for any one of the specified patterns separated by |, such as: cat, dog, or fish. |
. |
Locate a single occurrence of any character. |
^ |
Locates a match only at the beginning of a string, as in: ^Hello. |
$ |
Locates a match at the end of a string, as in: World$. |
\d |
Locate a numeric digit. |
\s |
Locate a whitespace character. |
\b |
Match \bWORD at the beginning of a word, or WORD\b at the end of a word. |
\uxxxx |
Locate the Unicode character represented by the hexadecimal number xxxx. |
Quantifiers specify quantities.
Quantifier |
Description |
n+ |
Matches any string containing at least one instance of the character “n”. |
n* |
Matches any string containing zero or more occurrences of the character “n”. |
n? |
Matches any string containing zero or one occurrence of the character “n”. |
n{x} |
Matches any string containing a sequence of “X” occurrences of the character “n”. |
n{x,y} |
Matches any string containing a sequence of “X” to “Y” occurrences of the character “n”. |
n{x,} |
Matches any string containing a sequence of at least “X” occurrences of the character “n”. |