Maruthi Krishna has Published 951 Articles

Posix character classes p{Lower} Java regex

Maruthi Krishna

Maruthi Krishna

Updated on 21-Nov-2019 08:14:19

190 Views

This class matches the lower case alphabetic characters i.e. a to z.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Posix_LowerExample {    public static void main( String args[] ) {       //Regular expression to match lower case letters       String regex = "^\p{Lower}+$";     ... Read More

How to remove non-ASCII characters from strings

Maruthi Krishna

Maruthi Krishna

Updated on 21-Nov-2019 08:10:22

916 Views

The Posix character class \p{ASCII} matches the ASCII characters and the meta character ^ acts as negation.i.e. The following expression matches all the non-ASCII characters."[^\p{ASCII}]"The replaceAll() method of the String class accepts a regular expression and a replacement-string and, replaces the characters of the current string (matching the given pattern) with ... Read More

How to match the regex meta characters in java as literal characters.

Maruthi Krishna

Maruthi Krishna

Updated on 21-Nov-2019 08:08:32

199 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.The filed LITERAL of the enables literal parsing of the pattern. i.e. all the regular expression metacharacters and escape sequences don’t have any special meaning they ... Read More

How match a string irrespective of case using Java regex.

Maruthi Krishna

Maruthi Krishna

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

150 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

14K+ 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

11K+ 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

How to extract each (English) word from a string using regular expression in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 21-Nov-2019 07:57:30

1K+ Views

The regular expression “[a-zA-Z]+” matches one or the English alphabet. Therefore, to extract each word in the given input string −Compile the above expression of the compile() method of the Pattern class.Get the Matcher object bypassing the required input string as a parameter to the matcher() method of the Pattern ... Read More

Program to check valid mobile number using Java regular expressions

Maruthi Krishna

Maruthi Krishna

Updated on 21-Nov-2019 07:50:55

4K+ Views

You can match a valid mobile number using the following regular expression −"\d{10}"A valid mobile number generally have 10 digits (in India).The metacharacter "\d" matches the digits from 0 to 9.The quantifier ex{n} suggests the repetition of ex n times.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PhoneNumberExample { ... Read More

Java Regular expression to check if a string contains alphabet

Maruthi Krishna

Maruthi Krishna

Updated on 21-Nov-2019 07:46:41

4K+ Views

Following is the regular expression to match alphabet in the given input −"^[a-zA-Z]*$"Where, ^ matches the starting of the sentence.[a-zA-z] matches the lower case and upper case letters.* indicates the occurrence for zero or more times.& indicates the end of the line.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ... Read More

How to remove white spaces using Java Regular Expression (RegEx)

Maruthi Krishna

Maruthi Krishna

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

1K+ Views

The regular expression “\s” matches the spaces in a string. The replaceAll() method accepts a string and a regular expression replaces the matched characters with the given string. To remove all the white spaces from an input string, invoke the replaceAll() method on it bypassing the above mentioned regular expression ... Read More

Advertisements