How to use Regular Expressions in Java – Syntaxes
Many languages like Java, Python, C#, Perl, Groovy and others support Regular Expressions in their own way. Let us see how regular expressions are used and the syntax of different regular expressions in Java.
Following are the List of Syntaxes
Expression | Description |
---|---|
^ | beginning of line. |
$ | end of line. |
. | any single character except newline. |
[…] | any single character in brackets. |
[^…] | any single character not in brackets |
A | Beginning of entire string |
z | End of entire string |
Z | End of entire string except allowable final line terminator. |
re* | 0 or more occurrences of preceding expression. |
re+ | 1 or more of the previous thing |
re? | 0 or 1 occurrence of preceding expression. |
re{ n} | exactly n number of occurrences of preceding expression. |
re{ n,} | n or more occurrences of preceding expression. |
re{ n, m} | at least n and at most m occurrences of preceding expression. |
a|b | either a or b. |
(re) | Groups regular expressions and remembers matched text. |
(?: re) | Groups regular expressions without remembering matched text. |
(?> re) | independent pattern without backtracking. |
w | word characters. |
W | nonword characters. |
s | whitespace. Equivalent to [tnrf]. |
S | nonwhitespace. |
d | digits. Equivalent to [0-9]. |
D | nondigits. |
A | beginning of string. |
Z | end of string. If a newline exists, it matches just before newline. |
z | end of string. |
G | point where last match finished. |
n | Back-reference to capture group number “n” |
b | word boundaries when outside brackets. Matches backspace (0x08) when inside brackets. |
B | nonword boundaries. |
n, t, etc. | newlines, carriage returns, tabs, etc. |
Q | Escape (quote) all characters up to E |
E | Ends quoting begun with Q |
Using the above syntaxes we can use regular expressions in Java.
Enjoy Reading This Article?
Here are some more articles you might like to read next: