
- 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
Is Java matcher thread safe in Java?
A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data. Java provides the java.util.regex package for pattern matching with regular expressions.
Matcher class
A Matcher object is the engine that interprets the pattern and performs match operations against an input string. Like the Pattern class, Matcher defines no public constructors. You obtain a Matcher object by invoking the matcher() method on a Pattern object.
The Instances of this class are not safe for use by multiple concurrent threads.
Example
Following Java program accepts 5 strings from the user and prints the strings starting with a digit.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class StartingwithDigit { public static void main( String args[] ) { String regex = "^[0-9].*$"; Scanner sc = new Scanner(System.in); System.out.println("Enter 5 input strings: "); String input[] = new String[5]; for (int i=0; i<5; i++) { input[i] = sc.nextLine(); } //Creating a Pattern object Pattern p = Pattern.compile(regex); System.out.println("Strings starting with digits: "); for(int i=0; i<5;i++) { //Creating a Matcher object Matcher m = p.matcher(input[i]); if(m.matches()) { System.out.println(m.group()); } } } }
Output
Enter 5 input strings: sample string 1 sample string 2 11 sample string 3 22 sample string 4 43534 56353 636 Strings starting with digits: 11 sample string 3 22 sample string 4 43534 56353 636
- Related Articles
- Is Swing thread-safe in Java?
- How to understand StringBuffer is thread-safe and StringBuilder is non-thread-safe in Java?\n
- Which collection classes are thread-safe in Java?
- How to make a collection thread safe in java?
- How to make a class thread-safe in Java?
- Thread-Safe collections in C#
- Thread Safe Concurrent Collection in C#
- Check if ArrayList is Synchronized (thread safe) in C#
- User Thread vs Daemon Thread in Java?
- Make your collections thread-safe in C#
- Thread Pools in Java
- Daemon thread in Java
- What is the Thread class in Java?
- What is a daemon thread in Java?
- Reset the Matcher in Java Regular Expressions
