Maruthi Krishna has Published 558 Articles

Posix character classes p{Lu} Java regex

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 12:12:39

303 Views

This class \p{Lu} matches upper case letters.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example1 {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a string");       Scanner sc = new Scanner(System.in);       String ... Read More

How to print all the characters of a string using regular expression in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 11:58:07

2K+ Views

The meta character "." matches all the characters, to print all the characters using the regular expressions −Compile the regular expression using the compile() method.Create a Matcher object using the matcher() method.Find the matches using the find() method and for every match print the matched contents (characters) using the group() ... Read More

Java regex program to split a string at every space and punctuation.

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 11:53:38

2K+ Views

The regular expression "[!._, '@?//s]" matches all the punctuation marks and spaces.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test {    public static void main( String args[] ) {       String input = "This is!a.sample"text, with punctuation!marks";       Pattern p = Pattern.compile("[!._, '@?//s]");     ... Read More

List.replaceAll(UnaryOperator operator) method in Java

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 11:41:15

2K+ Views

The replaceAll() method of the List interface accept an object of the UnaryOperator representing a particular operation, performs the specified operation on all the elements of the current list and replaces the existing values in the list with their respective results.Example Live Demoimport java.util.ArrayList; import java.util.function.UnaryOperator; class Op implements UnaryOperator { ... Read More

Java regular expression program to validate an email including blank field valid as well

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 11:39:06

324 Views

Following regular expression matches given e-mail id including the blank input −^([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2, 6})?$Where, ^ matches the starting of the sentence.[a-zA-Z0-9._%+-] matches one character from English alphabet (both cases), digits, "+", "_", ".", "" and, "-" before the @ symbol.+ indicates the repetition of the above mentioned set of characters one ... Read More

How to match bold fields in a HTML script using a regular expression in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 11:21:29

428 Views

The regular expression "\S" matches a non-whitespace character and the following regular expression matches one or more non space characters between the bold tags."(\S+)"Therefore to match the bold fields in a HTML script you need to −Compile the above regular expression using the compile() method.Retrieve the matcher from the obtained ... Read More

Regular Expression E Metacharacter in Java.

Maruthi Krishna

Maruthi Krishna

Updated on 10-Jan-2020 11:12:56

622 Views

The subexpression/metacharacter “\E” ends the quoting begun with \Q. i.e. you can escape metacharacters in the regular expressions by placing them in between \Q and \E. For example, the expression [aeiou] matches the strings with vowel letters in it.Example Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SampleProgram {   ... Read More

How match a string irrespective of case using Java regex.

Maruthi Krishna

Maruthi Krishna

Updated on 21-Nov-2019 08:06:13

292 Views

The compile method of the patter class accepts two parameters −A string value representing the regular expression.An integer value a field of the Pattern class.This CASE_INSENSITIVE field of the Pattern class matches characters irrespective of case. Therefore, if you pass as flag value to the compile() method along with your ... Read More

How to extract numbers from a string using regular expressions?

Maruthi Krishna

Maruthi Krishna

Updated on 21-Nov-2019 08:03:39

16K+ Views

You can match numbers in the given string using either of the following regular expressions −“\d+” Or, "([0-9]+)"Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ExtractingDigits {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter sample text: "); ... Read More

How to replace multiple spaces in a string using a single space using Java regex?

Maruthi Krishna

Maruthi Krishna

Updated on 21-Nov-2019 07:59:21

13K+ Views

The metacharacter “\s” matches spaces and + indicates the occurrence of the spaces one or more times, therefore, the regular expression \S+ matches all the space characters (single or multiple). Therefore, to replace multiple spaces with a single space.Match the input string with the above regular expression and replace the ... Read More

Advertisements