Found 7442 Articles for Java

Matcher pattern() method in Java with Examples

Maruthi Krishna
Updated on 20-Nov-2019 06:26:02

361 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 pattern() method of this (Matcher) class fetches and returns a Pattern (object) interpreted by the current Matcher.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PatternExample {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter your date of birth (MM/DD/YYY)");       String date = sc.next();       String regex = "^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$";   ... Read More

Pattern asPredicate() method in Java with examples

Maruthi Krishna
Updated on 20-Nov-2019 06:23:35

313 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

Pattern UNICODE_CHARACTER_CLASS field in Java with examples

Maruthi Krishna
Updated on 06-Dec-2023 12:41:53

684 Views

Enables the Unicode version of Predefined character classes and POSIX character classes. Example import java.util.regex.Matcher; import java.util.regex.Pattern; public class UNICODE_CHARACTER_CLASS_Example { public static void main( String args[] ) { String regex = "\u00de"; //Compiling the regular expression Pattern pattern = Pattern.compile(regex, Pattern.UNICODE_CHARACTER_CLASS); //Retrieving the matcher object String str[] = {"\u00de", "\u00fe", "\u00ee", "\u00ce"}; for (String ele : str) { Matcher matcher = pattern.matcher(ele); ... Read More

Pattern UNIX_LINES field in Java with Examples

Maruthi Krishna
Updated on 08-Aug-2024 12:56:52

297 Views

This flag enables Unix lines mode. In the Unix lines mode, only '' is used as a line terminator and ‘\r’ is treated as a literal character. Example 1 import java.util.regex.Matcher; import java.util.regex.Pattern; public class LTERAL_Example { public static void main(String[] args) { String input = "This is the first line\r" + "This is the second line\r" + "This is the third line\r"; //Regular expression to accept ... Read More

Pattern UNICODE_CASE field in Java with Examples

Maruthi Krishna
Updated on 07-Dec-2023 10:03:09

1K+ Views

Enables Unicode-aware case folding. When you use this as flag value to the compile() method along with the CASE_INSENSITIVE flag and if you search for Unicode characters using regular expressions Unicode characters of both cases will be matched. Example import java.util.regex.Matcher; import java.util.regex.Pattern; public class UNICODE_CASE_Example { public static void main( String args[] ) { String regex = "\u00de"; //Compiling the regular expression Pattern pattern = Pattern.compile(regex, Pattern.UNICODE_CASE|Pattern.CASE_INSENSITIVE); //Retrieving the matcher object String str[] = {"\u00de", "\u00fe", ... Read More

Pattern MULTILINE field in Java with examples

Maruthi Krishna
Updated on 05-Dec-2023 10:40:19

3K+ Views

Enables multiline mode in general, the ^ and $ meta characters matches the start and end of the given input with the specified characters irrespective of the number of lines in it. Example 1 import java.util.regex.Matcher; import java.util.regex.Pattern; public class MULTILINE_Example { public static void main( String args[] ) { //String regex = "(^This)";//.*t$)"; String input = "2234 This is a sample text" + "1424 This second 2335 line" + "This id third 455 line" ... Read More

How to convert JsonNode to ArrayNode using Jackson API in Java?

raja
Updated on 09-Jul-2020 07:59:35

18K+ Views

A JsonNode is a base class for all JSON nodes that forms the JSON Tree Model whereas ArrayNode is a node class that represents an array mapped from JSON content. We can convert or translate JsonNode to ArrayNode by typecasting the ArrayNode to retrieve the values using the readTree() method of ObjectMapper class and get() method for accessing the value of a specified element of an array node.Syntaxpublic JsonNode readTree(String content) throws IOException, com.fasterxml.jackson.core.JsonProcessingExceptionExampleimport com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.core.JsonProcessingException; public class JSonNodeToArrayNodeTest {    public static void main(String args[]) throws JsonProcessingException {       String jsonStr = "{\"Technologies\" : [\"Java\", ... Read More

Explain the Java regular expression construct "re?".

Maruthi Krishna
Updated on 19-Nov-2019 10:17:36

182 Views

The subexpression/metacharacter “re?” matches 0 or 1 occurrence of the preceding expression.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "Wel?";       String input = "Welcome to Tutorialspoint";       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(input);       int count = 0;       while(m.find()) {          count++;       }       System.out.println("Number of matches: "+count);    } }OutputNumber of matches: 1Example 2Following Java program accepts a string from ... Read More

Explain the Sub-Expression "(?> re)" in Java Regular Expressions

Maruthi Krishna
Updated on 19-Nov-2019 09:57:49

173 Views

The subexpression/metacharacter “(?> re)” matches the independent pattern without backtracking.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PatternExample {    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.next();       String regex = "(?>[0-9])";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       //verifying whether match occurred       boolean ... Read More

Sub-Expression "(?: re)" in Java Regular Expressions

Maruthi Krishna
Updated on 19-Nov-2019 09:56:16

188 Views

The subexpression/metacharacter “(?: re)” groups regular expressions without remembering the matched text.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PatternExample {    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.next();       String regex = "(?:[0-9])";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       //verifying whether match occurred     ... Read More

Advertisements