- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Regex to match lines containing multiple strings in Java
Example
import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SearchRegex { private Pattern subPattern = Pattern.compile(SUBJECT_PATTERN); private Matcher matcher; private static final String SUBJECT_PATTERN = "(?s)Subject 1:\s(.*)Subject 2:"; public static void main(String[] args) { String d = "Subject 1: Java
" + "Subject 2: Python"; SearchRegex obj = new SearchRegex(); List list = obj.getSubject(d); System.out.println("Address Result : " + list); } private List getSubject(String d){ List result = new ArrayList(); matcher = subPattern.matcher(d); while (matcher.find()) { result.add(matcher.group(1)); } return result; } }
Output
Address Result : [Java]
- Related Articles
- How to match a line not containing a word in Java Regex
- MySQL RegEx to find lines containing N semi-colons?
- How to match pattern over multiple lines in Python?
- How to Match patterns and strings using the RegEx module in Python
- Java regex program to match parenthesis "(" or, ")".
- How to match any character using Java RegEx
- How to match word characters using Java RegEx?
- How to match word boundaries using Java RegEx?
- Match all occurrences of a regex in Java
- How to match digits using Java Regular Expression (RegEx)
- How to match non-word boundaries using Java RegEx?
- Concatenate Multiple Strings in Java.
- Using Regex find strings containing a-z, A-Z and 0-9 in MySQL
- How to match a range of characters using Java regex
- How to match a non-word character using Java RegEx?

Advertisements