Match Non-Digits Using Java Regular Expression (Regex)

Maruthi Krishna
Updated on 19-Nov-2019 07:59:06

1K+ Views

You can match non-digit character using the meta character "\D".Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a String");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       String regex = "\D";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       int count = 0;       ... Read More

Match Digits Using Java Regular Expression (Regex)

Maruthi Krishna
Updated on 19-Nov-2019 07:56:43

3K+ Views

You can match digits in a given string using the meta character "\d" or by using the following expression : [0-9]Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a String");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       String regex = "\d";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input); ... Read More

Match Non-White Space Equivalent Using Java Regex

Maruthi Krishna
Updated on 19-Nov-2019 07:51:10

1K+ Views

You can match the non-white space characters using the meta character "\S".Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a String");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       String regex = "\S";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       int count = 0;     ... Read More

Match White Space Equivalent Using Java Regex

Maruthi Krishna
Updated on 19-Nov-2019 07:49:12

2K+ Views

The metacharacter "\s" matches the white space characters in the given string.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a String");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       String regex = "\s";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       int count = 0;   ... Read More

Match Non-Word Character Using Java Regex

Maruthi Krishna
Updated on 19-Nov-2019 07:44:37

2K+ Views

All the characters other than the English alphabet (both cases) and, digits (0 to 9) are considered as non-word characters. You can match them using the meta character “\W”.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a String");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       String regex = "^\W{5}";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the ... Read More

Match Word Characters Using Java Regex

Maruthi Krishna
Updated on 19-Nov-2019 07:41:03

987 Views

The English alphabet (both cases) and, digits (0 to 9) are considered as word characters. You can match them using the meta character “\w”.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a String");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       String regex = "^\w{5}";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       ... Read More

Match One of Two Expressions Using Java Regex

Maruthi Krishna
Updated on 19-Nov-2019 07:38:28

2K+ Views

Using the or logical operator | of Java regular expressions you can match either of two given expressions.For example, if you need your regular expression should match more than one expression you can do so by separating the required expressions by “|”.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a String");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       //Regular expression to match string that starts with hello or ... Read More

Match N Number of Occurrences of an Expression Using Java Regex

Maruthi Krishna
Updated on 19-Nov-2019 07:35:53

2K+ Views

The greedy quantifiers provided by Java allows you to match the multiple occurrences of an expression. Where, Exp{n} impels the occurrence of the expression exp exactly n times.Exp{n, } impels the occurrence of the expression exp at least n times.Exp{n, m} impels occurrence of the expression exp at least n and utmost m times.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       //regular expression to accept 5 letter word       String regex = "\w{5}";       Scanner sc = new Scanner(System.in);     ... Read More

Match Beginning of a String Line Using Java Regex

Maruthi Krishna
Updated on 19-Nov-2019 07:31:11

11K+ Views

The meta character “^” matches the beginning of a particular string i.e. it matches the first character of the string. For example, The expression “^\d” matches the string/line starting with a digit.The expression “^[a-z]” matches the string/line starting with a lower case alphabet.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a String");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       String regex = "^[^a-zA-Z0-9//s].*";       //Compiling the ... Read More

Match End of a Particular String Line Using Java Regex

Maruthi Krishna
Updated on 19-Nov-2019 07:27:26

3K+ Views

The meta character “$” matches the end of a particular string i.e. it matches the last character of the string. For example, The expression “\d$” matches the string/line ending with a digit.The expression “[a-z]$” matches the string/line ending with a lower case alphabet.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a String");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       String regex = ".*[^a-zA-Z0-9//s]$";       //Compiling the ... Read More

Advertisements