Regular Expression matches multiple line example – Java

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

  1. Emacs Multiline Regexp example
  2. Wikipedia : Regular Expression/

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
5 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
5 Comment authors
Muhammed ÖZDO?ANpoojaRads19RahulAbraão Isvi Recent comment authors
newest oldest most voted
Abraão Isvi
Guest
Abraão Isvi

Thanks for sharing!

Rads19
Guest
Rads19

Hi Mkyong,

Could you please give me your suggestion on this please.

http://www.webmd.com/search/search_results/default.aspx?query=dizziness%20drowsiness%20lightheadedness%20

With this pattern matching expression [A-Za-z0-9_\-\+\.\~\(\)\=\;\’\,] in the program code. I am unable to identify the above website link but I am not sure about how to include these patterns ‘?’, ‘%’ or a forward slash to identify a web site of this kind.

Could you please suggest me on this.

Thank you,
Radhika

pooja
Guest
pooja

great you saved my day 🙂

Rahul
Guest
Rahul

Kool

Muhammed ÖZDO?AN
Guest
Muhammed ÖZDO?AN

Thanks a ton Mkyong is lifesaver.