Java by API/java.util.regex/Matcher
Версия от 17:43, 31 мая 2010; (обсуждение)
Содержание
Matcher: appendReplacement(StringBuffer sb,String replacement)
/*
* Output:
gog gay gaughter gaut gig go gone gate
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
public static void main(String args[]) {
String joke = "dog day daughter daut did do done date";
String regEx = "d";
Pattern doggone = Pattern.rupile(regEx);
Matcher m = doggone.matcher(joke);
StringBuffer newJoke = new StringBuffer();
while (m.find())
m.appendReplacement(newJoke, "g");
m.appendTail(newJoke);
System.out.println(newJoke.toString());
}
}
Matcher.appendTail(StringBuffer sb)
/*
goat goats
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
public static void main(String[] av) {
String joke = "dog " + "dogs";
String regEx = "dog";
Pattern doggone = Pattern.rupile(regEx);
Matcher m = doggone.matcher(joke);
StringBuffer newJoke = new StringBuffer();
while (m.find()) {
m.appendReplacement(newJoke, "goat");
}
m.appendTail(newJoke);
System.out.println(newJoke.toString());
}
}
Matcher: find()
/*
* Output:
Looking for Java in Java 2.
subsequence found
* */
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
public static void main(String args[]) {
Pattern pat = Pattern.rupile("Java");
Matcher mat = pat.matcher("Java 2");
System.out.println("Looking for Java in Java 2.");
if (mat.find())
System.out.println("subsequence found");
else
System.out.println("No Match");
}
}
Matcher: group()
/*
* Output:
Match: X
Match: XX
Match: XXX
* */
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
public static void main(String args[]) {
Pattern pat = Pattern.rupile("X+");
Matcher mat = pat.matcher("X XX XXX");
while(mat.find())
System.out.println("Match: " + mat.group());
}
}
Matcher: groupCount()
/*
Group 0: 1
Group 1: 1
Group 2: null
Group 3: null
Group 0: 2
Group 1: 2
Group 2: null
Group 3: null
Group 0: 3
Group 1: 3
Group 2: null
Group 3: null
Group 0: 4.5
Group 1: 4.5
Group 2: .5
Group 3: null
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
public static void main(String[] av) {
String regEx = "[+|-]?(\\d+(\\.\\d*)?)|(\\.\\d+)";
String str = "a b c d e 1 2 3 4.5 ";
Pattern pattern = Pattern.rupile(regEx);
Matcher m = pattern.matcher(str);
while(m.find()) {
for(int i = 0; i<=m.groupCount() ; i++) {
System.out.println("Group " + i + ": " + m.group(i)); // Group i substring
}
}
}
}
Matcher: group(int group)
/*
Group 0: 1
Group 1: 1
Group 2: null
Group 3: null
Group 0: 2
Group 1: 2
Group 2: null
Group 3: null
Group 0: 3
Group 1: 3
Group 2: null
Group 3: null
Group 0: 4.5
Group 1: 4.5
Group 2: .5
Group 3: null
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
public static void main(String[] av) {
String regEx = "[+|-]?(\\d+(\\.\\d*)?)|(\\.\\d+)";
String str = "a b c d e 1 2 3 4.5 ";
Pattern pattern = Pattern.rupile(regEx);
Matcher m = pattern.matcher(str);
while(m.find()) {
for(int i = 0; i<=m.groupCount() ; i++) {
System.out.println("Group " + i + ": " + m.group(i)); // Group i substring
}
}
}
}
Matcher: lookingAt()
/*
IGNORE_CASE match true
MATCH_NORMAL match was false
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
public static void main(String[] argv) {
String pattern = "^q[^u]\\d+\\.";
String input = "QA777. is the next flight.";
Pattern reCaseInsens = Pattern.rupile(pattern, Pattern.CASE_INSENSITIVE);
Pattern reCaseSens = Pattern.rupile(pattern);
boolean found;
Matcher m;
m = reCaseInsens.matcher(input); // will match any case
found = m.lookingAt(); // will match any case
System.out.println("IGNORE_CASE match " + found);
m = reCaseSens.matcher(input); // Get matcher w/o case-insens flag
found = m.lookingAt(); // will match case-sensitively
System.out.println("MATCH_NORMAL match was " + found);
}
}
Matcher: matches()
/*
* Output:
Matches
No Match
* */
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
public static void main(String args[]) {
Pattern pat;
Matcher mat;
boolean found;
pat = Pattern.rupile("Java");
mat = pat.matcher("Java");
found = mat.matches();
if (found)
System.out.println("Matches");
else
System.out.println("No Match");
System.out.println();
mat = pat.matcher("Java 2");
found = mat.matches();
if (found)
System.out.println("Matches");
else
System.out.println("No Match");
}
}
Matcher: replaceAll(String text)
/*
* Output:
Original sequence: Java1 Java2 JDK jexp jexp.ru
Modified sequence: Java Java JDK Java jexp.ru
* */
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
public static void main(String args[]) {
String str = "Java1 Java2 JDK jexp jexp.ru";
Pattern pat = Pattern.rupile("Java.*? ");
Matcher mat = pat.matcher(str);
System.out.println("Original sequence: " + str);
str = mat.replaceAll("Java ");
System.out.println("Modified sequence: " + str);
}
}
Matcher: start()
/*
* Output:
java found at index 4
* */
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
public static void main(String args[]) {
Pattern pat = Pattern.rupile("java");
Matcher mat = pat.matcher("www.jexp.ru 1 2 3 j a v a 2 s.c o m");
while (mat.find()) {
System.out.println("java found at index " + mat.start());
}
}
}