Found 2616 Articles for Java

Pattern matches() method in Java with examples

Maruthi Krishna
Updated on 20-Nov-2019 05:44:56

463 Views

The java.util.regex package of java provides various classes to find particular patterns in character sequences. The pattern class of this package is a compiled representation of a regular expression.The matches() method of the Pattern class accepts −A string value representing the regular expression.An object of the CharSequence class representing the input string.On invocation, this method matches the input string against the regular expression. This method returns a boolean value which is true in-case of a match else, false.Exampleimport java.util.Scanner; import java.util.regex.Pattern; public class MatchesExample {    public static void main(String[] args) {       //Getting the date     ... Read More

Pattern quote() method in Java with examples

Maruthi Krishna
Updated on 20-Nov-2019 05:42:59

2K+ Views

The java.util.regex package of java provides various classes to find particular patterns in character sequences.The pattern class of this package is a compiled representation of a regular expression. The quote() method of this class accepts a string value and returns a pattern string that would match the given string i.e. to the given string additional metacharacters and escape sequences are added. Anyway, the meaning of the given string is not affected.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class QuoteExample {    public static void main( String args[] ) {       //Reading string value       Scanner ... Read More

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

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

13K+ 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

113 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

120 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

128 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

Regular Expression "(re)" Sub-Expression in Java

Maruthi Krishna
Updated on 19-Nov-2019 09:54:10

229 Views

The subexpression/metacharacter “( )” groups the regular expressions and remembers the matched text.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main( String args[] ) {       String input = "Hello how are you welcome to Tutorialspoint";       String regex = "H(ell|ow)";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       if(matcher.find()) {          System.out.println("Match found");       } else {       ... Read More

Explain the Metacharacter "B" in Java Regular Expressions.

Maruthi Krishna
Updated on 19-Nov-2019 09:50:33

155 Views

The subexpression/metacharacter “\B” matches the non-word boundaries.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = "\Bcause";       Scanner sc = new Scanner(System.in);       System.out.println("Enter a string: ");       String input = sc.nextLine();       Pattern p = Pattern.compile(regex);       Matcher m = p.matcher(input);       int count = 0;       while(m.find()) {          count ++;       }       System.out.println("Number of matches: "+count);    } ... Read More

How to match a character from given string including case using Java regex?

Maruthi Krishna
Updated on 19-Nov-2019 09:27:27

313 Views

The java.util.regex package of java provides various classes to find particular patterns in character sequences. The pattern class of this package is a compiled representation of a regular expression.To match a specific character from the given input string −Get the input string.This compile() method of this class accepts a string value representing a regular expression and an integer value representing a flag returns a Pattern object. Compile the regular expression bypassing −The pattern matcher “[ ]” with required character in it ex: “[t]”.The flag CASE_INSENSITIVE to ignore case.The matcher() method of the Pattern class accepts an input string and returns ... Read More

Pattern compile() method in Java with Examples

Maruthi Krishna
Updated on 19-Nov-2019 09:22:46

529 Views

The pattern class of the java.regex package is a compiled representation of a regular expression.The compile() method of this class accepts a string value representing a regular expression and returns a Pattern object.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CompileExample {    public static void main( String args[] ) {       //Reading string value       Scanner sc = new Scanner(System.in);       System.out.println("Enter input string");       String input = sc.nextLine();       //Regular expression to find digits       String regex = "(\d)";       //Compiling the regular expression ... Read More

Advertisements