
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Pattern COMMENTS field in Java with examples
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 1
import 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\n"; //Compiling the regular expression Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS); //Retrieving the matcher object Matcher matcher = pattern.matcher(input); int count = 0; String result = ""; while (matcher.find()) { count++; result = result+matcher.group(); } System.out.println("Number of digits in the given text: "+count); } }
Output
Enter input data: sample1 text2 with3 numbers4 in5 between6 Number of digits in the given text: 6
Example 2
import 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 your name: "); String name = sc.nextLine(); System.out.println("Enter your Date of birth: "); String dob = sc.nextLine(); //Regular expression to accept date in MM-DD-YYY format String regex = "^(1[0-2]|0[1-9])/ # For Month\n" + "(3[01]|[12][0-9]|0[1-9])/ # For Date\n" + "[0-9]{4}$ # For Year"; //Creating a Pattern object Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS); //Creating a Matcher object Matcher matcher = pattern.matcher(dob); boolean result = matcher.matches(); if(result) { System.out.println("Given date of birth is valid"); } else { System.out.println("Given date of birth is not valid"); } } }
Output
Enter your name: Krishna Enter your Date of birth: 09/26/1989 Given date of birth is valid
- Related Articles
- Pattern CANON_EQ field in Java with examples
- Pattern CASE_INSENSITIVE field in Java with examples
- Pattern DOTALL field in Java with examples
- Pattern LITERAL field in Java with examples
- Pattern MULTILINE field in Java with examples
- Pattern UNICODE_CASE field in Java with examples
- Pattern UNIX_LINES field in Java with examples
- Pattern UNICODE_CHARACTER_CLASS field in Java with examples
- Pattern pattern() method in Java with examples
- Pattern compile() method in Java with Examples
- Pattern matcher() method in Java with examples
- Pattern quote() method in Java with examples
- Pattern matches() method in Java with examples
- Pattern split() method in Java with examples
- Pattern splitAsStream() method in Java with examples

Advertisements