Found 2616 Articles for Java

How to match any character using Java RegEx

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

773 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 Metacharacter in Java

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

114 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

260 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 a|b Metacharacter in Java

Maruthi Krishna
Updated on 19-Nov-2019 06:40:39

285 Views

The subexpression/metacharacter “a| b” matches either a or b.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "Hello|welcome";       String input = "Hello how are you welcome to Tutorialspoint";       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 2The following Java program reads gender ... Read More

Regular Expression re{ n, m} Metacharacter in Java

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

150 Views

The subexpression/metacharacter “re{ n, m}” matches at least n and at most m occurrences of the preceding expression.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "xyy{2, 4}";       String input = "xxyyzxxyyyyxyyzxxyyzz";       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: 1Example 2The following ... Read More

Regular Expression re{ n} Metacharacter in Java

Maruthi Krishna
Updated on 19-Nov-2019 06:34:52

90 Views

The subexpression/metacharacter “re{ n}” matches exactly n number of occurrences of the preceding expression.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "to{1}";       String input = "Welcome to Tutorialspoint";       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 Java program reads age ... Read More

Regular Expression re+ Metacharacter in Java

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

89 Views

The subexpression/metacharacter “re+” matches one or more occurrences of the preceding expression.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "aabc+";       String input = "aabcabcaabcabbcaabcbcaabc";       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: 4Example 2import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test ... Read More

Regular Expression "\Z" Metacharacter in Java

Maruthi Krishna
Updated on 19-Nov-2019 06:21:36

422 Views

The subexpression/metacharacter “\Z” matches the end of the entire string except allowable final line terminator.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "Tutorialspoint\z";       String input = "Hi how are you welcome to Tutorialspoint";       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: 1Example ... Read More

Regular Expression "z" construct in Java

Maruthi Krishna
Updated on 19-Nov-2019 06:23:55

99 Views

The subexpression/metacharacter “\z” matches the end of a string.Example1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "Tutorialspoint\z";       String input = "Hi how are you welcome to Tutorialspoint";       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: 1Example2The following Java program verifies whether the given ... Read More

Regular Expression "A" construct in Java

Maruthi Krishna
Updated on 19-Nov-2019 06:18:18

143 Views

The subexpression/metacharacter “\A” matches the beginning of the entire string.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "\AHi";       String input = "Hi how are you welcome to Tutorialspoint";       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: 1Example 2Following Java program accepts a ... Read More

Advertisements