Enables
dotall mode. In dotall mode, the expression . matches any character,
including a line terminator. By default this expression does not
match line terminators.
For
Example, address is spawned into multiple lines.
String
data = "Address\n" +
"Country: India\n"
+
"State: AP\n" +
"City: Guntur\n"
+
"Village: Ongole"
+
Now
I want to extract upto the field city. Below statements do that.
static
Pattern addressPattern = Pattern.compile("(.*)Village:",
Pattern.DOTALL);
matcher
= addressPattern.matcher(data);
import java.util.regex.*; public class RegExHarness { static Pattern addressPattern = Pattern.compile("(.*)Village:", Pattern.DOTALL); static Matcher matcher; public static void main(String[] args) { String data = "Address\n" + "Country: India\n" + "State: AP\n" + "City: Guntur\n" + "Village: Ongole" + "mailId: 12345676@abc.com"; RegExHarness obj = new RegExHarness(); System.out.println(data + "\n"); matcher = addressPattern.matcher(data); while (matcher.find()) { System.out.println(matcher.group(1)); } } }
Output
Address Country: India State: AP City: Guntur Village: OngolemailId: 12345676@abc.com Address Country: India State: AP City: Guntur
No comments:
Post a Comment