Java Regular Expression Overview – Syntax

Regular expression (regex) defines patterns to recognise strings. Those patterns can be anything: a character, a specific string, a group of special characters or a combination of them. We can use Regular expression for searching, extracting, and modifying text.
This tutorial shows you an overview of Regular Expression Syntax.

I. Syntax
1. Characters

To define content of a regex pattern, we use many kinds of character classes:

character match abbreviation of
. any single character except newline
(we can use m option to match the newline)
\d a digit [0-9]
\D a non-digit [^0-9]
\s a whitespace character [ \t\n\x0B\f\r]
\S a non-whitespace character [^\s]
\w a word character [a-zA-Z_0-9]
\W a non-word character [^\w]

When using in real Java code, except ‘.’, we should double the ‘escape’ (\\) :

Example:
The pattern "Java Sample Approach provides \\w" will match:
-> “Java Sample Approach provides Java Technology, Spring Framework, Sample Code
with the result: “Java Sample Approach provides J

For that reason, Java regex provides another basic construct:

2. Quantifiers

Now we can specify the quantity of the character which need to be matched. These should be placed right after the specified character.

quantifier description abbreviation of
{x} exactly x times
{x,} at least x times
{x,y} at least x but not more than y times
* 0 or more {0,}
+ 1 or more {1,}
? 0 or 1 {0,1}

This is how we use them with specified characters

Example:
1. The pattern "Java Sample Approach provides \\w+" will match:
-> “Java Sample Approach provides Java Technology, Spring Framework, Sample Code
with the result: “Java Sample Approach provides Java

2. The pattern "Java Sample Approach provides .+" will match:
-> “Java Sample Approach provides Java Technology, Spring Framework, Sample Code
with the same string result: “Java Sample Approach provides Java Technology, Spring Framework, Sample Code

3. The pattern "Java Sample Approach provides .{15}" will match:
-> “Java Sample Approach provides Java Technology, Spring Framework, Sample Code
with the result: “Java Sample Approach provides Java Technology

3. Meta characters
meta char match
. any character
^ the beginning of the line
$ the end of the line
[abc] the letter a or b or c
[abc][xz] the letter a or b or c, then v or z
[^abc] any, except the letter a or b or c
[a-c1-8] the letter between a and c, or from 1 to 7
A|Z A or Z
() grouping

Example:
1. The pattern "(Java Technology|Spring Framework|Sample Code)" will match:
-> “Java Sample Approach provides Java Technology, Spring Framework, Sample Code
with the result containing 3 items: [Java Technology, Spring Framework, Sample Code]

2. The pattern "[a-zA-Z\\s]+" will match:
-> “Java Sample Approach provides Java Technology, Spring Framework, Sample Code
with the result containing 3 items: [Java Sample Approach provides Java Technology, {whitespace}Spring Framework, {whitespace}Sample Code]

II. Source code

To test your own code, just use:


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*