`
moor212
  • 浏览: 174090 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

正则表达式,实验

    博客分类:
  • java
阅读更多
学习基础,做了一些实验,现在将那些东西贴上。
package test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestPattern {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
testMatchTime();
}

public static void testFullStop() {
// "."匹配任意一个字符
String str1 = "a c";
String str2 = "abcddefc";
String regex = "a.c";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str1);
System.out.println("match result: " + m.matches());
}

public static void testSquareBrackets() {
// "[]"匹配[]里面的任意一个字符
String str1 = "acf";
String regex = "a[abcdef]f";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str1);
System.out.println("match result: " + m.matches());
}

public static void testOR() {
// "|"匹配或的关系,用小括号配合"或"关系
String str1 = "acf";
String regex = "a(a|b|c)f";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str1);
System.out.println("match result: " + m.matches());
}

public static void testMatchTime() {
// 测试匹配次数,?匹配0次或1次
String str2 = "abc";
String regex2 = "ab?c";
Pattern p2 = Pattern.compile(regex2);
Matcher m2 = p2.matcher(str2);
System.out.println("match result?: " + m2.matches());

// 测试匹配次数,*匹配0个或多个左边字符
String str1 = "aaaaaaf";
String regex1 = "a*f";
Pattern p = Pattern.compile(regex1);
Matcher m = p.matcher(str1);
System.out.println("match result*: " + m.matches());

// 测试匹配次数,+匹配1次或多次左侧紧挨的字符
String str3 = "aaab";
String regex3 = "a+b";
Pattern p3 = Pattern.compile(regex3);
Matcher m3 = p3.matcher(str3);
System.out.println("match result+: " + m3.matches());

//测试组合[]+,匹配小写字母
String str31 = "aaab";
String regex31 = "[a-z]+";
Pattern p31 = Pattern.compile(regex31);
Matcher m31 = p31.matcher(str31);
System.out.println("match result []+: " + m31.matches());

// 测试匹配次数,{n}恰好n次
String str4 = "aaab";
String regex4 = "a{3}b";
Pattern p4 = Pattern.compile(regex4);
Matcher m4 = p4.matcher(str4);
System.out.println("match result{n}: " + m4.matches());

// 测试匹配次数,{n,m}恰好n次~m次
String str5 = "aaaab";
String regex5 = "a{3,5}b";
Pattern p5 = Pattern.compile(regex5);
Matcher m5 = p5.matcher(str5);
System.out.println("match result{n,m}: " + m5.matches());

// 测试匹配次数,组合11-222-333
// "-"表示范围,"\"表示转义
String str6 = "11-222-3333";
String regex6 = "[0-9]{2}\\-[0-9]{3}\\-[0-9]{4}";
Pattern p6 = Pattern.compile(regex6);
Matcher m6 = p6.matcher(str6);
System.out.println("match result: " + m6.matches());

// 测试匹配次数,组合11-222-333
// "-"表示范围,"\"表示转义
String str7 = "11-2223333";
String regex7 = "[0-9]{2}\\-?[0-9]{3}\\-?[0-9]{4}";
Pattern p7 = Pattern.compile(regex7);
Matcher m7 = p7.matcher(str7);
System.out.println("match result: " + m7.matches());

}

public static void testNot() {
// 测试"^"符号,[^X],用在方括号内表示不想匹配的字符。
String str = "abc";
String regex = "[^de]bc";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
System.out.println("match result: " + m.matches());
}


public static void test_d() {
// 测试"\\d"符号,等价[0-9]。
String str = "1bc";
String regex = "[0-9]bc";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
System.out.println("match result: " + m.matches());

String str2 = "1bc";
String regex2= "\\dbc";
Pattern p2 = Pattern.compile(regex2);
Matcher m2 = p2.matcher(str2);
System.out.println("match result: " + m2.matches());
}

public static void test_w() {
// 测试"\\w"符号,等价[0-9]。
String str1 = "Abc";
String str2 = "1bc";
String regex = "[A-Z0-9]bc";

Pattern p1 = Pattern.compile(regex);
Matcher m1 = p1.matcher(str1);
System.out.println("match result: " + m1.matches());

Pattern p2 = Pattern.compile(regex);
Matcher m2 = p2.matcher(str2);
System.out.println("match result: " + m2.matches());

String str3 = "2bc";
String regex3= "\\wbc";
Pattern p3 = Pattern.compile(regex3);
Matcher m3 = p3.matcher(str1);
System.out.println("match result: " + m3.matches());
}


}


----------------------------
正则表达式匹配标点符号的方法
Posted by gezhe on 五月 25th 2011 @ 12:23 下午

str = str.replaceAll(“[\\pP‘’“”]“, “”);

Unicode 编码并不只是为某个字符简单定义了一个编码,而且还将其进行了归类。


\pP 其中的小写 p 是 property 的意思,表示 Unicode 属性,用于 Unicode 正表达式的前缀。

大写 P 表示 Unicode 字符集七个字符属性之一:标点字符。

其他六个是

L:字母;
M:标记符号(一般不会单独出现);
Z:分隔符(比如空格、换行等);
S:符号(比如数学符号、货币符号等);
N:数字(比如阿拉伯数字、罗马数字等);
C:其他字符

-----------------------------

应用:
1.匹配中间全角、半角空格(网上的一般匹配的都不太理想,这是本人原创):

                     Pattern p = null; // 样式
Matcher m = null; // 匹配者
boolean b = false;

                  String strVerify="校   对";
                  p = Pattern.compile("校[\\pZ\\s]+对");
                  m = p.matcher(strVerify);
b = m.matches();
System.out.println(b);
==========================================================

正则表达式的关系:
包含关系
        String strVerify="dddd份  数:  打  印:  校          对:ddddddd ";

p = Pattern.compile("校[\\pZ\\s]+对");
m = p.matcher(strVerify);
if(m.find()){
System.out.println("HHH");
}

匹配关系:
前面,多有所述,不再累述。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics