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

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

900 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 and an empty string as inputs.Example 1public class RemovingWhiteSpaces {    public static void main( String args[] ) {       String input = "Hi welcome to tutorialspoint";       String regex = "\s";       String result = input.replaceAll(regex, "");       System.out.println("Result: "+result);   ... Read More

Program to find whether a string is alphanumeric.

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 method returns true else it returns false.Therefore, to find whether a particular string contains alpha-numeric values −Get the string.Invoke the match method on it bypassing the above mentioned regular expression.Retrieve the result.Example 1import java.util.Scanner; public class AlphanumericString {    public static void main(String args[]) {       Scanner sc ... Read More

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

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

1K+ 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 replaceAll() method.Example 1public class RemovingVowels {    public static void main( String args[] ) {       String input = "Hi welcome to tutorialspoint";       String regex = "[aeiouAEIOU]";       String result = input.replaceAll(regex, "");       System.out.println("Result: "+result);    } }OutputResult: H wlcm ... Read More

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

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

1K+ 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 input string."([^aeiouyAEIOUY0-9\W]+)";Then you can remove the matched characters by replacing them with the empty string “”, using the replaceAll() method.Example 1public class RemovingConstants {    public static void main( String args[] ) {       String input = "Hi welc#ome to t$utori$alspoint";       String regex = "([^aeiouAEIOU0-9\W]+)"; ... Read More

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

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

3K+ 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 {    public static void main( String args[] ) {       String regex = "[aeiouAEIOU]";       System.out.println("Enter input string: ");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       //Compiling the regular expression       Pattern.compile(regex); ... Read More

Moving all Upper case letters to the end of the String using Java RegEx

Maruthi Krishna
Updated on 21-Nov-2019 07:29:15

558 Views

The subexpression “[ ]” matches all the characters specified in the braces. Therefore, to move all the upper case letters to the end of a string −Iterate through all the characters in the given string.Match all the upper case letters in the given string using the regular expression "[A-Z]".Concatenate the special characters and the remaining characters to two different strings.Finally, concatenate the special characters string to the other string.Example 1public class RemovingSpecialCharacters {    public static void main(String args[]) {       String input = "sample B text C with G upper case LM characters in between";     ... Read More

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

Maruthi Krishna
Updated on 21-Nov-2019 07:26:50

951 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

How to match a particular word in a string using Pattern class in Java?

Maruthi Krishna
Updated on 21-Nov-2019 07:24:54

883 Views

The \b meta character in Java regular expressions matches the word boundaries Therefore to find a particular word from the given input text specify the required word within the word boundaries in the regular expressions as −"\brequired word\b";Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MachingWordExample1 {    public static void main( String args[] ) {       //Reading string value       Scanner sc = new Scanner(System.in);       System.out.println("Enter input string");       String input = sc.next();       //Regular expression to find digits       String regex = "\bhello\b";   ... Read More

Matcher usePattern() method in Java with Examples

Maruthi Krishna
Updated on 21-Nov-2019 07:22:43

48 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.The usePattern() method of the Matcher class accepts a Pattern object representing a new regex pattern and uses it to find matches.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class UsePatternExample {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");       String input = sc.nextLine();       String regex = "[#%&*]"; ... Read More

Java program to convert Byte array to IP Address

Sunidhi Bansal
Updated on 20-Nov-2019 13:10:10

679 Views

Given with a Byte array ad the task is to convert it into an IP address using the IPAddress class in java and display the result.What is a Byte ArrayA byte comprises of 8 bits and byte array comprises of contiguous bytes which stores the binary information. In java, byte is a primitive datatype that can be understood as the byte of computer i.e. it is of 8 bits and it can hold values ranging for -128 to 127.Declaring a byte − byte name_of_byte_variable = initializer;Declaring a byte array − byte[] name_of_byte_array = new byte[];What is an IPAddress ClassIn java, ... Read More

Advertisements