Java Regular Expression
Java Regex or Regular Expression is an API to define Pattern for searching or manipulating Strings, In simple word we can say that Regular expression is a representation of a group of String object according to a particular Pattern.
Java provides the java.util.regex package for pattern matching with regular expressions.
java.util.regex package consists of following three classes :
Pattern Class
Public static Pattern compile(String)
Matcher Class
Important Methods of Matcher class
Pattern Syntax Exception
A pattern checked Exception is an Unchecked Exception that indicates a syntax error in Regular Expression.
Character Classes
Java provides the java.util.regex package for pattern matching with regular expressions.
java.util.regex package consists of following three classes :
Pattern Class
- In Pattern class Pattern object is a compiled version of a regular expression.
- It contains a compile() method which is used to create a Pattern.
- Pattern class provides no Public constructors.
Public static Pattern compile(String)
Matcher Class
- Matcher object match the given pattern in the target String
- Matcher object is created by matcher() method of Pattern Class
Important Methods of Matcher class
| S. No. | Method | Description |
|---|---|---|
| 1 | boolean find() | Returns boolean value after match |
| 2 | int start() | Returns the start index of the previous match. |
| 3 | int end() | Returns the offset after the last character matched. |
A pattern checked Exception is an Unchecked Exception that indicates a syntax error in Regular Expression.
Character Classes
| Classes | Description |
|---|---|
| [abc] | Either a or b or c |
| [^abc] | Except a,b and c |
| [a-z] | Any Lower case Alphabet Symbol |
| [A-Z] | Any Upper case Alphabet Symbol |
| [a-zA-Z] | Any Alphabet Symbol |
| [0-9] | Any Digit from 0 to 9 |
| [a-zA-Z0-9] | Any Alphabet Symbol |
| [^a-zA-Z0-9] | Except Alpha Numeric Symbols |
import java.util.regex.*;
public class RegexDemo {
public static void main(String a[]) {
Pattern p = Pattern.compile("x");
Matcher m = p.matcher("ba9j@z# 9");
while (m.find()) {
System.out.println(m.start() + "...................." + m.group());
}
}
}
Now, we will provide value of x which is our above character classes and after this we will obtain following outputs
| x | Out Put |
|---|---|
| [abc] | 0....................b |
| 1....................a | |
| [^abc] | 2....................9 |
| 3....................j | |
| 4....................@ | |
| 5....................z | |
| 6....................# | |
| 7.................... | |
| 8....................9 | |
| [a-z] | 0....................b |
| 1....................a | |
| 3....................j | |
| 5....................z | |
| [A-Z] | |
| [a-zA-Z] | 0....................b |
| 1....................a | |
| 3....................j | |
| 5....................z | |
| [0-9] | 2....................9 |
| 8....................9 | |
| [a-zA-Z0-9] | 0....................b |
| 1....................a | |
| 2....................9 | |
| 3....................j | |
| 5....................z | |
| 8....................9 | |
| [^a-zA-Z0-9] | 4....................@ |
| 6....................# | |
| 7.................... |
Pre-Defined Character Classes
| Classes | Description |
|---|---|
| \s | Space Character |
| \S | Any Character Except Space |
| \d | Any Digit from [0-9] |
| \D | Any Character Except Digit |
| \w | Any word charatcer |
| \W | Except word Character |
| . | Any Character |
Clear Concept with Example
import java.util.regex.*;
public class RegexDemo2 {
public static void main(String[] args) {
Pattern p = Pattern.compile("x");
Matcher m = p.matcher("l3c J@8");
while (m.find()) {
System.out.println(m.start() + "...................." + m.group());
}
}
}
}
Here, value of x is continuously changing and w.r.t we obtained different values which is shown below in tabular form.
| x | Out Put |
|---|---|
| \\s | 3.................... |
| \\S | 0....................l |
| 1....................3 | |
| 2....................c | |
| 4....................J | |
| 5....................@ | |
| 6....................8 | |
| \\d | 1....................3 |
| 6....................8 | |
| \\D | 0....................l |
| 2....................c | |
| 3.................... | |
| 4....................J | |
| 5....................@ | |
| \\w | 0....................l |
| 1....................3 | |
| 2....................c | |
| 4....................J | |
| 6....................8 | |
| \\W | 3.................... |
| 5....................@ | |
| . | 0....................l |
| 1....................3 | |
| 2....................c | |
| 3.................... | |
| 4....................J | |
| 5....................@ | |
| 6....................8 |
Quantifiers
Quantifiers are used to perform number of occurrence of a specific pattern.
| Classes | Description |
|---|---|
| a | Exactly one a |
| a+ | At least one a |
| a* | Any number of a including Zero also |
| a? | At least one a |

Comments
Post a Comment