시소당
Pattern.CANON_EQ
/*
?l matches input ?l
?l matches input e?gal
?l does not match input e?gal
?l does not match input e'gal
?l does not match input e?l
* */
import java.util.regex.Pattern;
/**
* CanonEqDemo - show use of Pattern.CANON_EQ, by comparing varous ways of
* entering the Spanish word for "equal" and see if they are considered equal by
* the RE-matching engine.
*
* @version
$Id: CanonEqDemo.java,v 1.3 2004/03/21 20:06:20 ian Exp $
*/
public class MainClass {
public static void main(String[] args) {
String pattStr = "\u00e9gal"; // ?l
String[] input = { "\u00e9gal", // ?l - this one had better match :-)
"e\u0301gal", // e + "Combining acute accent"
"e\u02cagal", // e + "modifier letter acute accent"
"e'gal", // e + single quote
"e\u00b4gal", // e + Latin-1 "acute"
};
Pattern pattern = Pattern.compile(pattStr, Pattern.CANON_EQ);
for (int i = 0; i < input.length; i++) {
if (pattern.matcher(input[i]).matches()) {
System.out.println(pattStr + " matches input " + input[i]);
} else {
System.out.println(pattStr + " does not match input " + input[i]);
}
}
}
}
--다음페이지--
Pattern.CASE_INSENSITIVE
/*
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.compile(pattern, Pattern.CASE_INSENSITIVE);
Pattern reCaseSens = Pattern.compile(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);
}
}
--다음페이지--
Pattern: compile('d.*ian')
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
public static void main(String[] args) {
Pattern myRE = Pattern.compile("d.*ian");
Matcher matcher = myRE.matcher("danian devonian dan");
matcher.lookingAt();
String result = matcher.group(0);
System.out.println(result);
}
}
--다음페이지--
Pattern: compile('(\\w+)\\s(\\d+)')
import java.util.regex.*;
public class MainClass {
public static void main(String[] args) {
Pattern patt = Pattern.compile("(\\w+)\\s(\\d+)");
Matcher matcher = patt.matcher("Bananas 123");
matcher.lookingAt();
System.out.println("Name: " + matcher.group(1));
System.out.println("Number: " + matcher.group(2));
}
}
--다음페이지--
Pattern: compile('[a-z]+')
/*
* Output:
Match: www
Match: java
Match: s
Match: com
* */
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
public static void main(String args[]) {
// Match lowercase words.
Pattern pat = Pattern.compile("[a-z]+");
Matcher mat = pat.matcher("www.java2s.com.");
while (mat.find())
System.out.println("Match: " + mat.group());
}
}
--다음페이지--
Pattern: matcher(String text)
/*
* 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.compile("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");
}
}
--다음페이지--
Pattern: split(String str)
/*
* Output:
Next token: www
Next token: java2s
Next token: com
Next token: java
Next token: javascript
Next token: API
Next token: example
* */
import java.util.regex.Pattern;
public class MainClass {
public static void main(String args[]) {
Pattern pat = Pattern.compile("[ ,.!]");
String strs[] = pat.split("www.java2s.com java javascript API example.");
for (int i = 0; i < strs.length; i++)
System.out.println("Next token: " + strs[i]);
}
}