Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 42 of 50

Moving all special char to the end of the String using Java Regular Expression RegEx)

Maruthi Krishna
Maruthi Krishna
Updated on 21-Nov-2019 2K+ Views

The following regular expression matches all the special characters i.e. all characters except English alphabet spaces and digits."[^a-zA-Z0-9\s+]"To move all the special characters to the end of the given line, match all the special characters using this regex concatenate them to an empty string and concatenate remaining characters to another string finally, concatenate these two strings.Example 1public class RemovingSpecialCharacters {    public static void main(String args[]) {       String input = "sample # text * with & special@ characters";       String regex = "[^a-zA-Z0-9\s+]";       String specialChars = "";       String inputData ...

Read More

What is difference between matches() and find() in Java Regex?

Maruthi Krishna
Maruthi Krishna
Updated on 20-Nov-2019 741 Views

The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class, you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.Both matches() and find() methods of the Matcher class tries to find the match according to the regular expression in the input string. In case of a match, both returns true and if a match is not found both methods return false.The main difference is that the matches() method try to match the entire region of the given input i.e. if you are trying to search for ...

Read More

Pattern asPredicate() method in Java with examples

Maruthi Krishna
Maruthi Krishna
Updated on 20-Nov-2019 346 Views

The Predicate interface of the java.util.function package can be used as a target for lambda expressions. The test method of this interface accepts a value ad validates it with the current value of the Predicate object. This method returns true in-case of a match, else false.The asPredicate() method of the java.util.regex.Pattern class returns a Predicate object which can match a string with the regular expression using which the current Pattern object was compiled.Example 1import java.util.Scanner; import java.util.function.Predicate; import java.util.regex.Pattern; public class AsPredicateExample {    public static void main( String args[] ) {       //Reading string value     ...

Read More

Pattern DOTALL field in Java with examples

Maruthi Krishna
Maruthi Krishna
Updated on 20-Nov-2019 4K+ Views

The DOTALL field of the Pattern class Enables dotall mode. By default, the “.” Meta character in regular expressions matches all characters except line terminators.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class DOTALL_Example {    public static void main( String args[] ) {       String regex = ".";       String input = "this is a sample this is second line";       Pattern pattern = Pattern.compile(regex);       Matcher matcher = pattern.matcher(input);       int count =0;       while(matcher.find()) {          count++;          System.out.print(matcher.group());     ...

Read More

Pattern COMMENTS field in Java with examples

Maruthi Krishna
Maruthi Krishna
Updated on 20-Nov-2019 654 Views

The COMMENTS field of the Pattern class allows whitespace and comments in pattern. When you use this as flag value to the compile() method, white spaces and, comments starting with # are ignored in the given pattern.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class COMMENTES_Example {    public static void main( String args[] ) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input data: ");       String input = sc.nextLine();       //Regular expression to find digits       String regex = "\d #ignore this comment";       //Compiling ...

Read More

Pattern CASE_INSENSITIVE field in Java with examples

Maruthi Krishna
Maruthi Krishna
Updated on 20-Nov-2019 2K+ Views

This CASE_INSENSITIVE field of the Pattern class matches characters irrespective of case. When you use this as flag value to the compile() method and if you search for characters using regular expressions characters of both cases will be matched.Note − By default, this flag matches only ASCII charactersExample 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CASE_INSENSITIVE_Example {    public static void main( String args[] ) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input data: ");       String input = sc.nextLine();       System.out.println("Enter required character: ");       char ...

Read More

Pattern splitAsStream() method in Java with examples

Maruthi Krishna
Maruthi Krishna
Updated on 20-Nov-2019 448 Views

The Pattern class of the java.util.regex package is a compiled representation of a regular expression.The splitAsStream() method of this class accepts a CharSequence object, representing the input string as a parameter and, at each match, it splits the given string into a new substring and returns the result as a stream holding all the substrings.Exampleimport java.util.regex.Pattern; import java.util.stream.Stream; public class SplitAsStreamMethodExample {    public static void main( String args[] ) {       //Regular expression to find digits       String regex = "(\s)(\d)(\s)";       String input = " 1 Name:Radha, age:25 2 Name:Ramu, age:32" + ...

Read More

Explain the Java regular expression construct "re?".

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

The subexpression/metacharacter “re?” matches 0 or 1 occurrence 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 = "Wel?";       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: 1Example 2Following Java program accepts a string from ...

Read More

Explain the Sub-Expression "(?> re)" in Java Regular Expressions

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

The subexpression/metacharacter “(?> re)” matches the independent pattern without backtracking.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PatternExample {    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.next();       String regex = "(?>[0-9])";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       //verifying whether match occurred       boolean ...

Read More

Sub-Expression "(?: re)" in Java Regular Expressions

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

The subexpression/metacharacter “(?: re)” groups regular expressions without remembering the matched text.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PatternExample {    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.next();       String regex = "(?:[0-9])";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       //verifying whether match occurred     ...

Read More
Showing 411–420 of 500 articles
« Prev 1 40 41 42 43 44 50 Next »
Advertisements