Maruthi Krishna has Published 870 Articles

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

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

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

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

Program to find whether a string is alphanumeric.

Maruthi Krishna

Maruthi Krishna

Updated on 21-Nov-2019 07:40:53

2K+ Views

Any word that contains numbers and letters is known as alphanumeric. The following regular expression matches the combination of numbers and letters."^[a-zA-Z0-9]+$";The matches method of the String class accepts a regular expression (in the form of a String) and matches it with the current string in case the match this ... Read More

How to remove vowels from a string using regular expressions in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 21-Nov-2019 07:38:12

2K+ Views

The simple character class “[ ]” matches all the specified characters in it. The following expression matches the characters except for xyz."[xyz]"Similarly, the following expression matches all the vowels in the given input string."([^aeiouAEIOU0-9\W]+)";Then you can remove the matched characters by replacing them with the empty string “”, using the ... Read More

How to remove consonants from a string using regular expressions in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 21-Nov-2019 07:35:26

2K+ Views

The simple character class “[ ]” matches all the specified characters in it. The meta character ^ acts as negation within the above character class i.e. the following expression matches all the characters except b (including spaces and special characters)"[^b]"Similarly, the following expression matches all the consonants in the given ... Read More

Program to match vowels in a string using regular expression in Java

Maruthi Krishna

Maruthi Krishna

Updated on 21-Nov-2019 07:32:02

5K+ Views

You can group all the required characters to match within the square braces “[ ]” i.e. The metacharacter/sub-expression “[ ]” matches all the specified characters. Therefore, to match all the letters specify the vowel letters within these as shown below −[aeiouAEIOU]Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchVowels ... Read More

Advertisements