1 java 中的证则表达式
http://www.javaresearch.org/article/31233.htm
http://bbs.pfan.cn/post-290605.html
1.1 定义
正则表达式(Regular Expression)是一种生成字符串的字符串。晕吧。比如说,String regEx="me+";这里字符串me+能够生成的字符串是:me、mee、meee、meeeeeeeeee等等,一个正则表达式可能生成无穷的字符串,所以我们不可能(有必要吗?)输出正则表达式产生的所有东西。
反过来考虑,对于字符串:me、mee、meee、meeeeeeeeee等等,我们能否有一种语言去描述它们呢?显然,正则表达式语言是这种语言,它是一些字符串的模式??简洁而深刻的描述。
- import java.util.regex.*;
- public class MatcherTest {
- // 如果想在查找时忽略大小写,则可以写成Pattern
- // p=Pattern.compile(regEx,Pattern.CASE_INSENSITIVE);
- public static void main(String[] args) throws Exception {
- // example 1
- String regEx = ".+(.+)$";
- String str = "c://dir1//dir2//name.txt";
- Pattern p = Pattern.compile(regEx);
- Matcher m = p.matcher(str);
- m.find();
- for (int i = 1; i <= m.groupCount(); i++) {
- System.out.println(m.group(i));
- }
- System.out.println("");
- System.out.println("");
- System.out.println("==========================================");
- System.out.println(" 判断某个字符串是否 符合 正则表达式 ");
- System.out.println("==========================================");
- System.out.println("中文".matches("[//u4e00-//u9fa5]+"));
- System.out.println("ab".matches("[//u4e00-//u9fa5]+"));
- System.out.println("中2文".matches("[//u4e00-//u9fa5]+"));
- System.out.println("ab".matches("[//u4e00-//u9fa5]"));
- System.out.println("中2文".matches("[//u4e00-//u9fa5]"));
- System.out.println("中文".matches("[//u4e00-//u9fa5]"));
- System.out.println("");
- System.out.println("");
- System.out.println("==========================================");
- System.out.println(" 判断某个字符串是否 包含子字符串 正则表达式 ");
- System.out.println("==========================================");
- regEx = "[//u4e00-//u9fa5]";
- str = "c://dir1//dir2//name.txt中文";
- p = Pattern.compile(regEx);
- m = p.matcher(str);
- System.out.println(m.find());
- System.out.println("");
- System.out.println("");
- System.out.println("==========================================");
- System.out.println(" 查找所有符合正则表达式的子字符串 ");
- System.out.println(" (//d)(?=a) 表示a的前一位数字 ");
- System.out.println("==========================================");
- String candidate = "A M1atcher ex2amines the results of 3applying 4a p53attern.";
- String regex = "(//d)(?=a)";
- p = Pattern.compile(regex);
- m = p.matcher(candidate);
- String val = null;
- System.out.println("INPUT: " + candidate);
- System.out.println("REGEX: " + regex + "/r/n");
- while (m.find()) {
- val = m.group();
- System.out.println("MATCH: " + val);
- }
- System.out.println("");
- System.out.println("");
- System.out.println("");
- System.out.println("==========================================");
- System.out
- .println(" 根据这则表达式 分割 字符串 等效于 String[] r = /"xd::abc::cde/".split(/"::/"); ");
- System.out.println("==========================================");
- regEx = "::";
- str = "xd::abc::cde";
- p = Pattern.compile(regEx);
- System.out.println("INPUT: " + str);
- System.out.println("REGEX: " + regEx + "/r/n");
- String[] r = p.split(str);
- for (int i = 0; i < r.length; i++) {
- System.out.println("r[" + i + "] is " + r[i]);
- }
- System.out.println("");
- System.out.println("");
- System.out.println("");
- System.out.println("");
- System.out.println("==========================================");
- System.out.println(" 根据正则表达式替换字符串 ");
- System.out.println("==========================================");
- regEx = "a+"; // 表示一个或多个a
- str = "aaabbced a ccdeaa";
- p = Pattern.compile(regEx);
- m = p.matcher(str);
- String s = m.replaceAll("A");
- System.out.println("INPUT: " + str);
- System.out.println("REGEX: " + regEx + "/r/n");
- System.out.println(s);
- // 如果写成空串,既可达到删除的功能,比如:
- // String s=m.replaceAll("");
- // 结果为"bbced ccde"
- }
- }
1.3 扩展包 commons-validator.jar and jakarta-oro.jar
已经实现了一些常见字段的验证,如验证email,电话,中文,url等