Found 2616 Articles for Java

Pattern LITERAL field in Java with examples

Maruthi Krishna
Updated on 05-Dec-2023 10:52:51

1K+ Views

Enables literal parsing of the pattern. In this, all the characters including escape sequences and, meta-characters don’t have any special meaning they are treated as literal characters. For example, normally if you search for the regular expression “^This” in the given input text it matches the lines starting with the word "This". 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" + "This is the second line" ... Read More

Pattern DOTALL field in Java with examples

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

3K+ Views

The DOTALL field of the Pattern class Enables dotall mode. By default, the “.” Meta character in regular expressions matches all characters except line terminators.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class DOTALL_Example {    public static void main( String args[] ) {       String regex = ".";       String input = "this is a sample this is second line";       Pattern pattern = Pattern.compile(regex);       Matcher matcher = pattern.matcher(input);       int count =0;       while(matcher.find()) {          count++;          System.out.print(matcher.group());     ... Read More

Pattern COMMENTS field in Java with examples

Maruthi Krishna
Updated on 20-Nov-2019 06:07:37

445 Views

The COMMENTS field of the Pattern class allows whitespace and comments in pattern. When you use this as flag value to the compile() method, white spaces and, comments starting with # are ignored in the given pattern.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class COMMENTES_Example {    public static void main( String args[] ) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input data: ");       String input = sc.nextLine();       //Regular expression to find digits       String regex = "\d #ignore this comment";       //Compiling ... Read More

Pattern CASE_INSENSITIVE field in Java with examples

Maruthi Krishna
Updated on 20-Nov-2019 06:05:24

2K+ Views

This CASE_INSENSITIVE field of the Pattern class matches characters irrespective of case. When you use this as flag value to the compile() method and if you search for characters using regular expressions characters of both cases will be matched.Note − By default, this flag matches only ASCII charactersExample 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CASE_INSENSITIVE_Example {    public static void main( String args[] ) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input data: ");       String input = sc.nextLine();       System.out.println("Enter required character: ");       char ... Read More

Pattern CANON_EQ field in Java with examples

Maruthi Krishna
Updated on 06-Dec-2023 12:36:04

872 Views

The CANON_EQ field of the Pattern class matches two characters only if they are canonically equal. When you use this as flag value to the compile() method, two characters will be matched if and only if their full canonical decompositions are equal. Where canonical decomposition is one of the Unicode text normalization forms. Example 1 import java.util.regex.Matcher; import java.util.regex.Pattern; public class CANON_EQ_Example { public static void main( String args[] ) { String regex = "b\u0307"; //Compiling the regular expression Pattern pattern = Pattern.compile(regex, Pattern.CANON_EQ); ... Read More

Pattern flags() method in Java with examples

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

398 Views

The pattern class of 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, the following is the signature of this method.static Pattern compile(String regex)Another variant of this method accepts an integer value representing flags, following is the signature of the compile method with two parameters.static Pattern compile(String regex, int flags)The Pattern class provides various fields each representing a flagS.NoField and Description1CANON_EQMatches two characters only if they are canonically equal.2CASE_INSENSITIVEMatches characters irrespective of case.3COMMENTSAllows whitespace and comments in pattern.4DOTALLEnables dotall mode. Where ... Read More

Pattern toString() method in Java with examples

Maruthi Krishna
Updated on 20-Nov-2019 05:54:48

122 Views

The Pattern class of the java.util.regex package is a compiled representation of a regular expression.The toString() method of this class returns the string representation of the regular expression using which the current Pattern was compiled.Example1import java.util.Scanner; import java.util.regex.Pattern; public class Example {    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

Pattern splitAsStream() method in Java with examples

Maruthi Krishna
Updated on 20-Nov-2019 05:52:46

241 Views

The Pattern class of the java.util.regex package is a compiled representation of a regular expression.The splitAsStream() method of this class accepts a CharSequence object, representing the input string as a parameter and, at each match, it splits the given string into a new substring and returns the result as a stream holding all the substrings.Exampleimport java.util.regex.Pattern; import java.util.stream.Stream; public class SplitAsStreamMethodExample {    public static void main( String args[] ) {       //Regular expression to find digits       String regex = "(\s)(\d)(\s)";       String input = " 1 Name:Radha, age:25 2 Name:Ramu, age:32" + ... Read More

Pattern split() method in Java with examples

Maruthi Krishna
Updated on 20-Nov-2019 05:51:14

507 Views

The Pattern class of the java.util.regex package is a compiled representation of a regular expression.The split() method of this class accepts a CharSequence object, representing the input string as a parameter and, at each match, it splits the given string into a new token and returns the string array holding all the tokens.Exampleimport java.util.regex.Pattern; public class SplitMethodExample {    public static void main( String args[] ) {       //Regular expression to find digits       String regex = "(\s)(\d)(\s)";       String input = " 1 Name:Radha, age:25 2 Name:Ramu, age:32 3 Name:Rajev, age:45";     ... Read More

Pattern pattern() method in Java with examples

Maruthi Krishna
Updated on 20-Nov-2019 05:47:04

1K+ 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 pattern() method of the Pattern class fetches and returns the regular expression in the string format, using which the current pattern was compiled.Example 1import java.util.regex.Pattern; public class PatternExample {    public static void main(String[] args) {       String date = "12/09/2019";       String regex = "^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$";       //Creating a pattern object       Pattern pattern = Pattern.compile(regex);       if(pattern.matcher(date).matches()) {     ... Read More

Advertisements