Found 7442 Articles for Java

How to match word boundaries using Java RegEx?

Maruthi Krishna
Updated on 19-Nov-2019 08:06:01

483 Views

You can match the word boundaries using the meta character “\b”.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 = "\b";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       int count = 0;     ... Read More

How to match end of the input using Java RegEx?

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

558 Views

You can match the end of the input using the meta character “\z”.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 = "[0-9]\z";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       int count = 0;   ... Read More

Regular Expression Metacharacter in Java

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

193 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

321 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

410 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

212 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

149 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

165 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

498 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

159 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

Advertisements