Java Regex
The Java
Regex or Regular Expression is an API that defines a pattern for searching or
manipulating strings.
It is
widely used to define the constraint strings such as password and email
validation.
The Matcher and Pattern classes provide the facility of Java regular expression. The java.util.regex package provides the following classes and interfaces for regular expressions.
- 1.
MatchResult interface
- 2.
Matcher class
- 3.
Pattern class
- 4.
PatternSyntaxException class
Regex
Character classes
No. |
Character
Class |
Description |
1 |
[abc] |
a, b, or
c (simple class) |
2 |
[^abc] |
Any
character except a, b, or c (negation) |
3 |
[a-zA-Z] |
a
through z or A through Z, inclusive (range) |
4 |
[a-d[m-p]] |
a
through d, or m through p: [a-dm-p] (union) |
5 |
[a-z&&[def]] |
d, e, or
f (intersection) |
6 |
[a-z&&[^bc]] |
a
through z, except for b and c: [ad-z] (subtraction) |
7 |
[a-z&&[^m-p]] |
a
through z, and not m through p: [a-lq-z](subtraction) |
Regex Quantifiers
The
quantifiers specify the number of occurrences of a character.
Regex Description
X? |
X occurs once or not at all |
X+ |
X occurs once or more times |
X* |
X occurs zero or more times |
X{n} |
X occurs n times only |
X{n,} |
X occurs n or more times |
X{y,z} |
X occurs at least y times but less than z
times |
Regex Metacharacters
The
regular expression metacharacters work as shortcodes.
Regex Description
. |
Any character (may or may not match
terminator) |
\d |
Any digits, short of [0-9] |
\D |
Any non-digit, short for [^0-9] |
\s |
Any whitespace character, short for
[\t\n\x0B\f\r] |
\S |
Any non-whitespace character, short for
[^\s] |
\w |
Any word character, short for [a-zA-Z_0-9] |
\W |
Any non-word character, short for [^\w] |
\b |
A word boundary |
\B |
A non-word boundary |
package String;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main(String[] args) {
// Input text containing phone numbers
String inputText = "John: 123-456-7890, Jane: 987-654-3210, Bob: 555-1234";
// Define a regex pattern for matching phone numbers
String phoneRegex = "\\b\\d{3}-\\d{3}-\\d{4}\\b";
// Create a Pattern object
Pattern phonePattern = Pattern.compile(phoneRegex);
// Create a Matcher object
Matcher matcher = phonePattern.matcher(inputText);
// Find and print all matching phone numbers
System.out.println("Matching phone numbers:");
while (matcher.find()) {
System.out.println(matcher.group());
}
// Extract area codes from phone numbers using capturing groups
String areaCodeRegex = "\\b(\\d{3})-\\d{3}-\\d{4}\\b";
Pattern areaCodePattern = Pattern.compile(areaCodeRegex);
Matcher areaCodeMatcher = areaCodePattern.matcher(inputText);
// Find and print all area codes
System.out.println("\nExtracted area codes:");
while (areaCodeMatcher.find()) {
System.out.println("Area Code: " + areaCodeMatcher.group(1));
}
}
}
package String;
import java.util.regex.*;
public class RegexExample2 {
public static void main(String[] args) {
Pattern pat = Pattern.compile(" .m");
Matcher mat = pat.matcher(" .am");
Boolean bool = mat.matches();
System.out.println(bool); //return whether the pattern matches/not
Boolean bool1 = Pattern.matches(" .m", " .am");
System.out.println(bool1);
System.out.println(Pattern.matches("[amn]", "acd"));
System.out.println(Pattern.matches("[^amn]", "c"));
System.out.println(Pattern.matches("[a-zA-S]", "T"));
System.out.println(Pattern.matches("[MS][a-z]{5}", "Monica"));
System.out.println(Pattern.matches("[xyz]?", "x"));
System.out.println(Pattern.matches("[xyz]+", "x"));
System.out.println(Pattern.matches("[xyz]*", "xyyza"));
System.out.println(Pattern.matches("[\\d]", "1"));
System.out.println(Pattern.matches("[\\D]", "1"));
}
}
0 Comments