Matching Character with Regular Expression in Java is very easy.
Sample Java program to perform:
- Match
- Ignore
- Match in given range
- Union match
- Intersection match
- Subtraction match
package com.learning.regex;
public class RegexCharacterMain {
public static void main(String[] args) {
matchAnyGivenCharacter();
ignoreAllGivenCharacter();
matchCharacterRange();
unionMatchWithRange();
intersectionMatch();
subtractionMatch();
}
private static void matchAnyGivenCharacter() {
System.out.println("\n**************** matchAnyGivenCharacter *****************\n");
String regExPattern = "[abc]"; // matches a or b or c
String inputString = "b";
Boolean result = inputString.matches(regExPattern);
if (result) {
System.out.println(inputString + " : Character Match Found using Regular Expression " + regExPattern);
} else {
System.out.println(inputString + " : Character Match NOT Found using Regular Expression " + regExPattern);
}
}
private static void ignoreAllGivenCharacter() {
System.out.println("\n**************** ignoreInGivenCharacter *****************\n");
String regExPattern = "[^abc]"; // Ignore only a, b or c
String inputString = "a";
Boolean result = inputString.matches(regExPattern);
if (result) {
System.out.println(inputString + " : NOT Ignored in Regular Expression " + regExPattern);
} else {
System.out.println(inputString + " : Ignored in Regular Expression " + regExPattern);
}
}
private static void matchCharacterRange() {
System.out.println("\n**************** matchCharacterRange *****************\n");
String regExPattern = "[a-c]"; // Range, to match a, b or c
String inputString = "b";
Boolean result = inputString.matches(regExPattern);
if (result) {
System.out.println(inputString + " : Found in Range Regular Expression " + regExPattern);
} else {
System.out.println(inputString + " : NOT Found in Range Regular Expression " + regExPattern);
}
}
private static void unionMatchWithRange() {
System.out.println("\n**************** unionMatchWithRange *****************\n");
String regExPattern = "[a-c[f-h]]"; // Union, to match a, b, c, f, g, h
String inputString = "g";
Boolean result = inputString.matches(regExPattern);
if (result) {
System.out.println(inputString + " : Found in Union-Range Regular Expression " + regExPattern);
} else {
System.out.println(inputString + " : NOT Found in Union-Range Regular Expression " + regExPattern);
}
}
private static void intersectionMatch() {
System.out.println("\n**************** intersectionMatch *****************\n");
String regExPattern = "[a-c&&[b-c]]"; // intersection, to match b or c
String inputString = "c";
Boolean result = inputString.matches(regExPattern);
if (result) {
System.out.println(inputString + " : Found in intersection Regular Expression " + regExPattern);
} else {
System.out.println(inputString + " : NOT Found in intersection Regular Expression " + regExPattern);
}
}
private static void subtractionMatch() {
System.out.println("\n**************** subtractionMatch *****************\n");
String regExPattern = "[a-c&&[^b-c]]"; // Subtraction, to match a only
String inputString = "c";
Boolean result = inputString.matches(regExPattern);
if (result) {
System.out.println(inputString + " : Found in subtraction Regular Expression " + regExPattern);
} else {
System.out.println(inputString + " : NOT Found in subtraction Regular Expression " + regExPattern);
}
}
}
Sample Java program output:
**************** matchAnyGivenCharacter *****************
b : Character Match Found using Regular Expression [abc]
**************** ignoreInGivenCharacter *****************
a : Ignored in Regular Expression [^abc]
**************** matchCharacterRange *****************
b : Found in Range Regular Expression [a-c]
**************** unionMatchWithRange *****************
g : Found in Union-Range Regular Expression [a-c[f-h]]
**************** intersectionMatch *****************
c : Found in intersection Regular Expression [a-c&&[b-c]]
**************** subtractionMatch *****************
c : NOT Found in subtraction Regular Expression [a-c&&[^b-c]]