Found 7442 Articles for Java

Regular Expression E Metacharacter in Java.

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

584 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

124 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

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

Maruthi Krishna
Updated on 15-Jul-2025 17:58:40

5K+ Views

In Java, the strings that are used to find the pattern are known as regular expressions. In this article, we will learn to check multiple regex patterns against an input, and it can be done by using 2 approaches. Using Metacharacter Using the List object Using Metacharacter The meta character "|" in Java regular expressions allows you to match more than one regular expression. For example, if you need to match a particular input text with more than one expression, you need to separate them using the following: exp1|exp2|exp3 Example ... 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

376 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

436 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

MatchResult start(int group) method in Java with examples.

Maruthi Krishna
Updated on 10-Jan-2020 10:27:43

85 Views

The java.util.regex.MatcheResult interface provides methods to retrieve the results of a match.You can get an object of this interface using the toMatchResult() method of the Matcher class. This method returns a MatchResult object which represents the match state of the current matcher.The end(int group) method of this interface accepts an integer representing a particular group and returns the offset before the first match occurred in the specified group.Example Live Demoimport java.util.Scanner; import java.util.regex.MatchResult; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main( String args[] ) {       String regex = "(.*)(\d+)(.*)";       //Reading input ... Read More

Character class p{javaMirrored} Java regex.

Maruthi Krishna
Updated on 10-Jan-2020 10:20:10

407 Views

This character class \p{javaMirrored} matches upper case letters. This class matches the characters which returns true when passed as a parameter to the isMirrored() method of the java.lang.Character class.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    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 = "[\p{javaMirrored}]";       //Compiling the regular expression       Pattern pattern = ... Read More

Character class p{javaWhitespace} Java regex in java.

Maruthi Krishna
Updated on 10-Jan-2020 10:09:24

467 Views

This character class \p{javaWhitespace} matches spaces. This class matches the characters which returns true when passed as a parameter to the isWhitespace() method of the java.lang.Character class.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    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 = "[\p{javaWhitespace}]";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);   ... Read More

Character class p{javaUpperCase} Java regex.

Maruthi Krishna
Updated on 10-Jan-2020 10:05:40

691 Views

This character class \p{javaUpperCase} matches upper case letters. This class matches the characters which returns true when passed as a parameter to the isUpperCase() method of the java.lang.Character class.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    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 = "[\p{javaUpperCase}]";       //Compiling the regular expression       Pattern pattern = ... Read More

How to implement IntUnaryOperator using lambda in Java?

raja
Updated on 13-Jul-2020 13:05:16

934 Views

IntUnaryOperator represents a functional interface that takes an int value and returns another int value. It is defined in java.util.function package and used as an assignment target for a lambda expression or method reference. It contains one abstract method: applyAsInt(), two default methods: compose(), andThen() and one static method: identity().Syntax@FunctionalInterface public interface IntUnaryOperatorExampleimport java.util.function.IntUnaryOperator; public class IntUnaryOperatorLambdaTest1 {    public static void main(String[] args) {       IntUnaryOperator getSquare = intValue -> {    // lambda expression          int result = intValue * intValue;          System.out.println("Getting square: "+ result);          return result;       };   ... Read More

Advertisements