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

Match a Range of Characters Using Java Regex

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

1K+ Views

To match a range of characters i.e. to match all the characters between two specified characters in a sequence you can use the character class as [a-z]The expression “[a-zA-Z]” accepts any English alphabet.The expression “[0-9&&[^35]]” accepts numbers except 3 and 5.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]*$";       //Compiling the regular expression   ... Read More

Match Fixed Set of Characters Using Java Regex

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

778 Views

The character classes allow you to accept a single character from a fixed set of characters. For example, The expression “[tmp]” matches the characters t or, m or, p.The expression “[^tp]” matches the characters other than t or, p.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 the characters t or, m or, p     ... Read More

Match Any Character Using Java Regex

Maruthi Krishna
Updated on 19-Nov-2019 07:15:24

1K+ Views

The meta character “.” in java regular expression matches any character (single) it could be the alphabet, number or, any special character.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 any character       String regex = ".";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       ... Read More

Regular Expression T Metacharacter in Java

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

197 Views

The subexpression/metacharacter “\t” matches the tab spaces.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "\t";       Scanner sc = new Scanner(System.in);       System.out.println("Enter a string: ");       String input = sc.nextLine();       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(input);       int count = 0;       while(m.find()) {          count ++;       }       System.out.println("Number of tab spaces: "+count);   ... Read More

Regular Expression Metacharacter in Java

Maruthi Krishna
Updated on 19-Nov-2019 07:04:02

328 Views

The subexpression/metacharacter “\b” matches the word boundaries when outside the brackets. Matches the backspace (0x08) when inside the brackets.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "\bbecause\b";       Scanner sc = new Scanner(System.in);       System.out.println("Enter a string: ");       String input = sc.nextLine();       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(input);       int count = 0;       while(m.find()) {          count ++;   ... Read More

Regular Expression g Metacharacter in Java

Maruthi Krishna
Updated on 19-Nov-2019 06:54:10

881 Views

The subexpression/metacharacter “\G” matches the point where the last match finished.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "\G[0-9]";       Scanner sc = new Scanner(System.in);       System.out.println("Enter a string: ");       String input = sc.nextLine();       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(input);       int count = 0;       String digits = "";       System.out.println("Digits in the previous match:");       while(m.find()) {   ... Read More

Regular Expression D Metacharacter in Java

Maruthi Krishna
Updated on 19-Nov-2019 06:53:22

664 Views

The subexpression/metacharacter “\D” matches the non-digits.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "\D";       String input = "This is sample text 12 24 56 89 24";       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(input);       int count = 0;       while(m.find()) {          count++;       }       System.out.println("Number of matches: "+count);    } }OutputNumber of matches: 24Example 2import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public ... Read More

Regular Expression D-Construct in Java

Maruthi Krishna
Updated on 19-Nov-2019 06:49:37

244 Views

The subexpression/metacharacter “\d” matches the digits.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "\d 24";       String input = "This is sample text 12 24 56 89 24";       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(input);       int count = 0;       while(m.find()) {          count++;       }       System.out.println("Number of matches: "+count);    } }OutputNumber of matches: 2Example 2Following is a Java program that ... Read More

Advertisements