May 10, 2021 Java
1. Java Regular Expression Tutorial - Java Regular Expression Meta-Characters
Meta-characters are characters that have a special meaning in Java regular expressions.
The meta-characters supported by regular expressions in Java are as follows:
( ) [ ] { { \ ^ $ | ? * + . < > - = !
The
[
,
]
specify the character class in the regular expression.
A character class is a set of characters. The regular expression engine will attempt to match a character in the collection.
The character class "ABC" will match the characters A, B, or C. For example, the string "woman" or "women" will match the regular expression "wom" (ae) n".
We can use character classes to specify a range of characters.
The range is represented by a
-
a character.
For example, any capital Letters are represented by the word
[A-Z]
"0-9" means any number between 0 and 9.
^
Indicates no.
For example,
[^ ABC]
A, B, and C is represented by .
The character
[^ A-Z]
any character other than the capital letter.
If
^
class appears, it matches only one character, except for
^
beginning.
For example, "ABC" will match A, B, C, or .
You can also include two or more ranges in a character class.
For example,
[a-zA-Z]
match a-z and A-to-Z.
[a-zA-Z0-9]
case) and any number 0 to 9.
The following table lists examples of character classes
Characters a to zThe character class | Meaning |
---|---|
[abc] | Characters a, b, or c |
[^xyz] | Characters other than x, y, and z |
[a-z] | |
[a-cx-z] | Characters a to c or x to z, which will include a, b, c, x, y or z. |
[0-9&&[4-8]] | The intersection of two ranges (4, 5, 6, 7 or 8), |
[a-z&&[^aeiou]] | All lowercase letters minus the phonetic tone |
The following table lists some commonly used predefined character classes.
PredefinedMeaning | |
---|---|
. | Any character |
\d | Numbers. Same as |
\D | Non-numbers. It's the same as |
\s |
Space character. I
ncludes the same as .
|
\S | Non-blank characters. It's the same as |
\w | One character. Same as a-zA-Z_0-9. |
\W | Non-word characters. It's the same as . |
The following code
\d
using .
\\d
to
\
. . .
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String args[]) { Pattern p = Pattern.compile("Java \\d"); String candidate = "Java 4"; Matcher m = p.matcher(candidate); if (m != null) System.out.println(m.find()); } }
The code above produces the following results.
The following
\w+
any word.
Double slashes are used to
\
.
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String args[]) { String regex = "\\w+"; Pattern pattern = Pattern.compile(regex); String candidate = "asdf Java2s.com"; Matcher matcher = pattern.matcher(candidate); if (matcher.find()) { System.out.println("GROUP 0:" + matcher.group(0)); } } }
The code above produces the following results.