Capturing groups are numbered by counting their opening parentheses from left to right. To find out how many groups are present in the expression, call the groupCount() method on a matcher object. The groupCount() method returns an int showing the number of capturing groups present in the matcher's pattern.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CountingGroupDemo {
public static void main(String[] args) {
//
// Define regex to find the word 'quick' or 'lazy' or 'dog'
//
String regex = "(quick)|(lazy)|(dog)";
String text = "the quick brown fox jumps over the lazy dog";
//
// Obtain the required matcher
//
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
int groupCount = matcher.groupCount();
System.out.println("Number of group = " + groupCount);
//
// Find every match and print it
//
while (matcher.find()) {
for (int i = 0; i <= groupCount; i++) {
//
// Group i substring
//
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Below is the output...
Number of group = 3
Group 0: quick
Group 1: quick
Group 2: null
Group 3: null
Group 0: lazy
Group 1: null
Group 2: lazy
Group 3: null
Group 0: dog
Group 1: null
Group 2: null
Group 3: dog
Showing posts with label Regex. Show all posts
Showing posts with label Regex. Show all posts
Tuesday, March 6, 2012
How do I count the number of capturing groups in java?
How do I combile character classes with quantifier in java?
This example show you how to attach quantifier to character classes or capturing group in regular expressions.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class CombineWithQuantifier {
public static void main(String[] args) {
//
// [abc]{3} --> apply quantifier in character class.
// Find 'a' or 'b' or 'c', three times in a row.
//
// (abc){3} --> apply quantifier in capturing group.
// Find 'abc', three times in a row.
//
// abc{3} --> apply quantifier in character class.
// Find character 'c', three times in a row.
//
String[] regexs = {"[abc]{3}", "(abc){3}", "abc{3}"};
String text = "abcabcabcabcaba";
for (String regex : regexs) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
//
// Find every match and print it
//
System.out.println("------------------------------");
System.out.format("Regex: %s %n", regex);
while (matcher.find()) {
System.out.format("Text \"%s\" found at %d to %d.%n",
matcher.group(), matcher.start(),
matcher.end());
}
}
}
}
Below is the output...
Regex: [abc]{3}
Text "abc" found at 0 to 3.
Text "abc" found at 3 to 6.
Text "abc" found at 6 to 9.
Text "abc" found at 9 to 12.
Text "aba" found at 12 to 15.
------------------------------
Regex: (abc){3}
Text "abcabcabc" found at 0 to 9.
------------------------------
Regex: abc{3}
How do I use reluctant quantifier regex in Java?
The reluctant quantifiers start the matcher at the beginning of the input string, then reluctantly eat one character at a time looking for a match. The last thing they try is the entire input string.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReluctantQuantifierDemo {
public static void main(String[] args) {
String[] regexs = {
"x??",
"x*?",
"x+?",
"x{2}?",
"x{2,}?",
"x{2,5}?"
};
String text = "xxxxxxx";
for (String r : regexs) {
Pattern pattern = Pattern.compile(r);
Matcher matcher = pattern.matcher(text);
//
// Find every match and print it
//
System.out.println("------------------------------");
System.out.format("regex: %s %n", r);
while (matcher.find()) {
System.out.format("Text \"%s\" found at %d to %d.%n",
matcher.group(), matcher.start(),
matcher.end());
}
}
}
}
How do I use possessive quantifier regex in java?
The possessive quantifiers always eat the entire input string, trying once (and only once) for a match. Unlike the greedy quantifiers, possessive quantifiers never back off, even if doing so would allow the overall match to succeed.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PossessiveQuantifierDemo {
public static void main(String[] args) {
String[] regexs = {
"x?+",
"x*+",
"x++",
"x{2}+",
"x{2,}+",
"x{2,5}+"
};
String input = "xxxxxxx";
for (String r : regexs) {
Pattern pattern = Pattern.compile(r);
Matcher matcher = pattern.matcher(input);
//
// Find every match and print it
//
System.out.println("------------------------------");
System.out.format("Regex: %s %n", r);
while (matcher.find()) {
System.out.format("Text \"%s\" found at %d to %d.%n",
matcher.group(), matcher.start(),
matcher.end());
}
}
}
}
How do I write embedded flag expression in Java?
It's also possible to enable various flags using embedded flag expressions. Embedded flag expressions are an alternative to the two-argument version of compile, and are specified in the regular expression itself. The example below is use (?i) flag expression to enable case-insensitive matching.
Another flag expressions are listed below:
(?x), equivalent with Pattern.COMMENTS(?m), equivalent with Pattern.MULTILINE(?s), equivalent with Pattern.DOTTAL(?u), equivalent with Pattern.UNICODE_CASE(?d), equivalent with Pattern.UNIX_LINES
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmbeddedFlagDemo {
public static void main(String[] args) {
//
// Define regex which starting with (?i) to enable
// case-insensitive matching
//
String regex = "(?i)the";
String text = "The quick brown fox jumps over the lazy dog";
//
// Obtain the required matcher
//
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
//
// Find every match and print it
//
while (matcher.find()) {
System.out.format("Text \"%s\" found at %d to %d.%n",
matcher.group(), matcher.start(),
matcher.end());
}
}
}
Below is the output...
Text "The" found at 0 to 3.
Text "the" found at 31 to 34.
Wednesday, February 1, 2012
How to filter particular tag from xml with the help of Regex Pattern in android?
In below example we considering multiple <CONTACT> tag in xml.........which is stored in "getxmlstring" variable
ArrayList<String> contactArray = new ArrayList<String>();
if (getxmlstring.contains("</CONTACT>")) {
Retrieve_Contact_Tag("<CONTACT></CONTACT>" + getxmlstring);
for (int i = 0; i < contactArray.size(); i++) {
String replacestr = contactArray.get(i).toString();
//Now parse the xml according to your Choice
parseContact("<CONTACT>" + replacestr + "</CONTACT>",context);
}
contactArray.clear();
}
private static void Retrieve_Contact_Tag(String xmlContent) {
Pattern regex = Pattern.compile("<CONTACT>(.*?)</CONTACT>",
Pattern.DOTALL);
Matcher matcher = regex.matcher(xmlContent);
// Pattern regex2 = Pattern.compile("<([^<>]+)>([^<>]+)</\\1>");
if (matcher.find()) {
while (matcher.find()) {
contactArray.add((matcher.group(1)));
}
}
}
ArrayList<String> contactArray = new ArrayList<String>();
if (getxmlstring.contains("</CONTACT>")) {
Retrieve_Contact_Tag("<CONTACT></CONTACT>" + getxmlstring);
for (int i = 0; i < contactArray.size(); i++) {
String replacestr = contactArray.get(i).toString();
//Now parse the xml according to your Choice
parseContact("<CONTACT>" + replacestr + "</CONTACT>",context);
}
contactArray.clear();
}
private static void Retrieve_Contact_Tag(String xmlContent) {
Pattern regex = Pattern.compile("<CONTACT>(.*?)</CONTACT>",
Pattern.DOTALL);
Matcher matcher = regex.matcher(xmlContent);
// Pattern regex2 = Pattern.compile("<([^<>]+)>([^<>]+)</\\1>");
if (matcher.find()) {
while (matcher.find()) {
contactArray.add((matcher.group(1)));
}
}
}
Subscribe to:
Posts (Atom)