Found 2616 Articles for Java

Collections.replaceAll() method and List.replaceAll() method in Java

Maruthi Krishna
Updated on 24-Feb-2020 07:00:17

3K+ Views

The replaceAll() method of Collections interface accepts a List object, two typed parameters representing old and new values, replaces the old values with the new values in the list.Example Live Demoimport java.util.ArrayList; import java.util.Collections; import java.util.List; public class ReplaceAllExample {    public static void main(String args[]) {       List list = new ArrayList();       list.add("Java");       list.add("Java Script");       list.add("HBase");       list.add("CoffeeScript");       list.add("TypeScript");       System.out.println("Contents of list: "+list);       Collections.replaceAll(list, "Java", "JAVA");       System.out.print("Contents of list after replace operation: "+list);    } ... Read More

Greedy quantifiers Java Regular expressions in java.

Maruthi Krishna
Updated on 10-Jan-2020 11:25:03

398 Views

Greedy quantifiers are the default quantifiers. A greedy quantifier matches as much as possible from the input string (longest match possible) if match not occurred it leaves the last character and matches again. Following is the list of greedy quantifiers −QuantifierDescriptionre*Matches zero or more occurrences.re?Matches zero or, 1 occurrence.re+Matches one or more occurrences.re{n}Matches exactly n occurrences.re{n, }Matches at least n occurrences.re{n, m}Matches at least n and at most m occurrences.ExampleIn the following Java example we are trying to match 1 or more digits, our input string is 45545 though the values 4, 45, 455 etc… are eligible, since we are ... Read More

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

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

304 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 pattern using the matcher() method.Print the matched parts of the input string using the group() method.Exampleimport java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main(String[] args) {      String str = "This is an example>/b> HTML script.";       //Regular expression to match contents of ... Read More

Regular Expression E Metacharacter in Java.

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

460 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 {    public static void main( String args[] ) {       String regex = "[aeiou]";       Scanner sc = new Scanner(System.in);       System.out.println("Enter input string: ");       String input = sc.nextLine();       //Creating a Pattern object       Pattern pattern = ... Read More

PatternSyntaxException class in Java regular expressions

Maruthi Krishna
Updated on 10-Jan-2020 11:07:19

88 Views

The PatternSyntaxException class represents an unchecked exception thrown in case of a syntax error in regex string. This class contains three main methods namely −getDescription() − Returns the description of the error.getIndex() − Returns the error index.getPattern() − Returns the regular expression pattern with error.getMessage() − Returns the complete message congaing the error, index, regular expression pattern with error, indication of the error in the pattern.Example Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; public class PatternSyntaxExceptionExample {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a String");       ... Read More

Java regex program to add space between a number and word in Java.

Maruthi Krishna
Updated on 10-Jan-2020 10:58:11

2K+ Views

You can form matched groups in the regular expression by separating the expressions with parenthesis. In following regular expression the first group matches digits and the second group matches the English alphabet −(\d)([A-Za-z])In short, it matches the part in the input string where a digit followed by an alphabet.Since the expression $1 indicates Group1 and $2 indicates Group2, if you replace The above Java regular expression with $1 $2, using the replace() method (of the String class) a space will be added between number and a word in the given input string when a number is followed by a word.Example Live ... Read More

Java regex program to verify whether a String contains at least one alphanumeric character.

Maruthi Krishna
Updated on 10-Jan-2020 10:53:56

7K+ Views

Following regular expression matches a string that contains at least one alphanumeric characters −"^.*[a-zA-Z0-9]+.*$";Where, ^.* Matches the string starting with zero or more (any) characters.[a-zA-Z0-9]+ Matches at least one alpha-numeric character..*$ Matches the string ending with zero or more (ant) characters.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a string");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       //Regular expression       String regex = "^.*[a-zA-Z0-9]+.*$";   ... Read More

How to check multiple regex patterns against an input? Using Java.

Maruthi Krishna
Updated on 10-Jan-2020 10:47:35

4K+ Views

The "|" meta character: The meta character "|" in Java regular expressions allows you to match more than one regular expressions for example if you need to match a particular input text with more than one expression you need to separate them using it as:exp1|exp2|exp3Example Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SampleExample {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter your input: ");       String input = sc.nextLine();       //Regular expression       String regex = "\d{10}|^[aeiou]";       //Creating ... Read More

Regular expression for a hexadecimal number greater than 10 and should be even in length in java.

Maruthi Krishna
Updated on 13-Jul-2020 13:15:09

291 Views

Following is the regular expression to match hexadecimal number greater than 10 with even length −^(?=.{10,255}$)(?:0x)?\p{XDigit}{2}(?:\p{XDigit}{2})*$Where,^ − Matches the starting of the sentence.(?=.{10,255}$) − String ending with characters with 10 to 255.\p{XDigit}{2} − Two hexa-decimal characters.(?:\p{XDigit}{2})* − 0 or more sequences of double hexa-decimal characters.$ − Matches the end of the sentence.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class JavaExample51 {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       String nums[] = new String[5];       for(int i=0; i

Custom UnaryOperator implementation in java.

Maruthi Krishna
Updated on 20-Feb-2020 12:04:09

306 Views

The java.util.function.UnaryOperator interface and can be used as assignment target for lambda expressions, it represents operation on a single operand whose result will be of same type as the input. We can create our own UnaryOperator by implementing this interface.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 with the resultant values.In the following example we are implementing the UnaryOperator interface and creating a custom unary operator object and trying to pass it as an ... Read More

Advertisements