
- 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
Finding a Match Within Another Match Java regular expressions
To match a pattern within another match you need to compile the regular expression to match the outer pattern find the match retrieve the results and pass the results as input to the inner Matcher object.
Example
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherExample { public static void main(String[] args) { int start = 0, len = -1; Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); String regexOuter = "<b>(.*?)</b>"; String regexInner = "\d+"; //Creating a pattern object Pattern patternOuter = Pattern.compile(regexOuter); Pattern patternInner = Pattern.compile(regexInner); //Matching the compiled pattern in the String Matcher outerMatcher = patternOuter.matcher(input); while (outerMatcher.find()) { Matcher innerMatcher = patternInner.matcher(outerMatcher.group(1)); while(innerMatcher.find()){ System.out.println(innerMatcher.group()); } } } }
Output
Enter input text: This is sample HTML data 123 sample text hello 123
- Related Articles
- How to match whitespace in python using regular expressions
- How to match whitespace but not newlines using Python regular expressions?
- MySQL Regular expressions: How to match digits in the string with d?
- How to match digits using Java Regular Expression (RegEx)
- PHP – Match regular expression using mb_ereg_match()
- How to match non-digits using Java Regular Expression (RegEx)
- How to use regular expression in Java to pattern match?
- MongoDB regular expression to match a specific record?
- Program to match vowels in a string using regular expression in Java
- How to match a regular expression against a string?
- Explain Python regular expression search vs match
- Replace one string with another string with Java Regular Expressions
- Replace all words with another string with Java Regular Expressions
- How to match one of the two given expressions using Java RegEx?
- Java Regular Expression to match a line that doesn't contain a word.

Advertisements