Java Articles

Page 358 of 450

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

Maruthi Krishna
Maruthi Krishna
Updated on 21-Nov-2019 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 class.Finally, for each match get the matched characters by invoking the group() method.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class EachWordExample {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter sample text: ");       String data = ...

Read More

Program to check valid mobile number using Java regular expressions

Maruthi Krishna
Maruthi Krishna
Updated on 21-Nov-2019 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 {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter your name: ");       String name = sc.nextLine();       System.out.println("Enter your Phone number: ");       String phone = sc.next();       //Regular expression to ...

Read More

Java Regular expression to check if a string contains alphabet

Maruthi Krishna
Maruthi Krishna
Updated on 21-Nov-2019 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 ContainsAlphabetExample {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       String names[] = new String[5];       for(int i=0; i

Read More

Program to find whether a string is alphanumeric.

Maruthi Krishna
Maruthi Krishna
Updated on 21-Nov-2019 3K+ 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 consonants from a string using regular expressions in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 21-Nov-2019 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 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
Maruthi Krishna
Updated on 21-Nov-2019 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 {    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
Maruthi Krishna
Updated on 21-Nov-2019 825 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
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 751 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 353 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
Showing 3571–3580 of 4,498 articles
« Prev 1 356 357 358 359 360 450 Next »
Advertisements