Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 43 of 50

Regular Expression "(re)" Sub-Expression in Java

Maruthi Krishna
Maruthi Krishna
Updated on 19-Nov-2019 384 Views

The subexpression/metacharacter “( )” groups the regular expressions and remembers the matched text.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main( String args[] ) {       String input = "Hello how are you welcome to Tutorialspoint";       String regex = "H(ell|ow)";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       if(matcher.find()) {          System.out.println("Match found");       } else {       ...

Read More

Explain the Metacharacter "B" in Java Regular Expressions.

Maruthi Krishna
Maruthi Krishna
Updated on 19-Nov-2019 270 Views

The subexpression/metacharacter “\B” matches the non-word boundaries.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 = "\Bcause";       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 matches: "+count);    } ...

Read More

How to match a character from given string including case using Java regex?

Maruthi Krishna
Maruthi Krishna
Updated on 19-Nov-2019 439 Views

The java.util.regex package of java provides various classes to find particular patterns in character sequences. The pattern class of this package is a compiled representation of a regular expression.To match a specific character from the given input string −Get the input string.This compile() method of this class accepts a string value representing a regular expression and an integer value representing a flag returns a Pattern object. Compile the regular expression bypassing −The pattern matcher “[ ]” with required character in it ex: “[t]”.The flag CASE_INSENSITIVE to ignore case.The matcher() method of the Pattern class accepts an input string and returns ...

Read More

How to match word boundaries using Java RegEx?

Maruthi Krishna
Maruthi Krishna
Updated on 19-Nov-2019 539 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 non-digits using Java Regular Expression (RegEx)

Maruthi Krishna
Maruthi Krishna
Updated on 19-Nov-2019 2K+ 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

How to match digits using Java Regular Expression (RegEx)

Maruthi Krishna
Maruthi Krishna
Updated on 19-Nov-2019 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

How to match word characters using Java RegEx?

Maruthi Krishna
Maruthi Krishna
Updated on 19-Nov-2019 1K+ 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

How to match one of the two given expressions using Java RegEx?

Maruthi Krishna
Maruthi Krishna
Updated on 19-Nov-2019 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

How to match beginning of a particular string/line using Java RegEx

Maruthi Krishna
Maruthi Krishna
Updated on 19-Nov-2019 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

How to match end of a particular string/line using Java RegEx

Maruthi Krishna
Maruthi Krishna
Updated on 19-Nov-2019 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
Showing 421–430 of 500 articles
« Prev 1 41 42 43 44 45 50 Next »
Advertisements