
- 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
Replacing all the matched contents Java regular expressions
Once you compile the required regular expression and retrieved the matcher object by passing the input string as a parameter to the matcher() method.
You can replace all the matched parts of the input string with another str4ing using the replaceAll() method of the Matcher class.
This method accepts a string (replacement string) and replaces all the matches in the input string with it and returns the result.
Example 1
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ReplaceAll{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); String regex = "\t+"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(input); int count = 0; while (matcher.find()) { count++; } System.out.println("No.of matches: "+count); String result = matcher.replaceAll(" "); System.out.println("Result: "+result); } }
Output
Enter input text: sample text with tab spaces No.of matches: 4 Result: sample text with tab spaces
In the same way you can replace the first match using the replaceFirst() method of the Matcher class.
Example 2
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ReplaceAll{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); String regex = "\d"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(input); int count = 0; while (matcher.find()) { count++; } System.out.println("No.of matches: "+count); String result = matcher.replaceFirst("#"); System.out.println("Result: "+result); } }
Output
Enter input text: test data 1 2 3 No.of matches: 3 Result: test data # 2 3
- Related Articles
- Getting the list of all the matches Java regular expressions
- How to get text contents of all matched elements using jQuery?
- Java Regular Expressions Tutorial
- Replace all words with another string with Java Regular Expressions
- Java regular expressions sample examples
- Possessive quantifiers Java Regular expressions
- Java Regular expressions Logical operators
- Reluctant quantifiers Java Regular expressions
- Use the ? quantifier in Java Regular Expressions
- Reset the Matcher in Java Regular Expressions
- Greedy quantifiers Java Regular expressions in java.
- Validate Phone with Java Regular Expressions
- Back references in Java regular expressions
- Regular Expressions syntax in Java Regex
- Regex quantifiers in Java Regular Expressions

Advertisements