Regular Expression matches multiple line example Java
By:Roy.LiuLast updated:2019-08-18
By default, the “.” doesn’t match line breaks. To match multiple lines, add (?s) prefix or enable the Pattern.DOTALL flag.
1. Example
A address sample, we need to extract the “Address 1” only.
Starting... Address 1: 88 app 2/8 superman taman, puchong 36100, Malaysia Address 2: abc End
Failed :
To extract “Address 1”, this pattern will return nothing, the “.” doesn’t match multiple lines.
Address 1:\\s(.*)Address 2:
Correct :
To extract “Address 1”, add (?s) prefix.
(?s)Address 1:\\s(.*)Address 2:
Or enable the Pattern.DOTALL mode.
Pattern.compile(Address 1:\\s(.*)Address 2:, Pattern.DOTALL);
2. RegEx Example
Example to use regex multiple lines matching to get the “Address 1” .
package com.mkyong.regex
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RunExampleTest{
private Pattern addressPattern = Pattern.compile(ADDRESS_PATTERN);
private Matcher matcher;
//Alternative
/*private Pattern addressPattern =
Pattern.compile(ADDRESS_PATTERN, Pattern.DOTALL);*/
private static final String ADDRESS_PATTERN = "(?s)Address 1:\\s(.*)Address 2:";
public static void main(String[] args) {
String data = "Testing... \n" +
"Address 1: 88 app 2/8\n" +
"superman taman, puchong\n" +
"36100, Malaysia\n" +
"Address 2: abc" +
"testing end";
RunExampleTest obj = new RunExampleTest();
List<String> list = obj.getAddress(data);
System.out.println("Test Data : ");
System.out.println(data + "\n");
System.out.println("Address Resut : " + list);
private List<String> getAddress(String data){
List<String> result = new ArrayList<String>();
matcher = addressPattern.matcher(data);
while (matcher.find()) {
result.add(matcher.group(1));
return result;
Output
Test Data : Testing... Address 1: 88 app 2/8 superman taman, puchong 47100, Malaysia Address 2: abctesting end Result : [88 app 2/8 superman taman, puchong 47100, Malaysia
References
From:一号门

COMMENTS