学习基础,做了一些实验,现在将那些东西贴上。
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");
}
匹配关系:
前面,多有所述,不再累述。
分享到:
相关推荐
这是编译原理的一个实验, 是把一个正则表达式转化为不确定有穷自动机NFA的算法程序,朋兴趣的朋友可以下载来看看哦. 一个正则表达式就是由普通字符(例如字符 a 到 z)以及特殊字符(称为元字符)组成的文字模式...
编译原理的一个实验,正则表达式到NFA
本书通过示例介绍如何编写正则表达式,旨在让读者轻松掌握正则表达式。事实上,笔者几乎将所涉及的每一个概念都通过示例展示了出来,读者很容易模仿尝试。正则表达式有助于找到文本字符串中的各种模式。更确切地说,...
C#正则表达式专题学习(很不错)附正则测试工具C#正则表达式专题学习(很不错)附正则测试工具
java正则表达式 word版 很实用,有图形有文字,愿意和大家分享。 regx
匹配 a 标签的正则表达式是\\*href=.*\\>匹配链接的的正则表达式是(href=\"([^\"]+)\")|(href='([^']*)')四、
python正则表达式_深入浅出
代码相对简单; c语言实现; 正则表达式转换为nfa;
本文档结合多年使用正则表达式经验,总结正则表达式使用方式以及常见的正则表达式的表达。希望有助于您的学习!
1. 熟练掌握字符串的创建 2. 掌握System.String类及其方法的使用 3. 掌握如何格式化字符串 4. 掌握正则表达式的用法
课程设计 正规式构造nfa.这是编译原理的一个实验, 是把一个正则表达式转化为不确定有穷自动机NFA的算法程序,朋兴趣的朋友可以下载来看看哦。
编译原理课的大作业 包含三个小实验 在一个cpp文件里 正则表达式转换为nfa nfa转换为dfa dfa最小化 个人原创代码
正则表达式学习小结 基础学习 正则表达式学习小结 基础学习
构造正则表达式的简化DFA算法论文 介绍了构造等价于给定正则表达式的简化确定有限自动机(DFA) 的算 法. 方法是首先构造与正则表达式等价的非确定有限自动机(NFA) , 这里省略了构 造带E动作的有限自动机的操作, 然后...
详细的解释了js中的正则表达式,代码加注释都有!
输入一个正则表达式,然后将此正则表达式转换为NFA,最后将此NFA输出 【实验要求】: 写出程序运行环境, 体会,程序结构,程序清单, 典型结果,以及结果分析 【程序运行环境】: 此程序是在Visual C++下,新建一...
C#计算器实验~ -使用正则表达式解决~ -依据windows自带计算器为参考~ -附有源程序~
正则表达式与自动机的转换,以及自动机的化简