/*以下是我最近研究正则表达式的成果希望能对大家有帮助。其中也有我遇到的不明白之处,各位有精通的请发mail给我讲讲。还有有谁对javascript的正则表达式精通请给大家发一下吧。这些东东都是javadoc上有的,还有一些是我在java.sun.com/docs/books/tutorial/extra/regex/index.html上看到的,我建议对正则表达式感兴趣的不妨上去看看,很不错。
*/
package testreg;
import java.util.regex.matcher;
import java.util.regex.pattern;
/**
* <p>title: 正则表达式的研究</p>
* <p>description:
* 最近在工作中常常用到一些正则表达式的使用问题,到网上去找介绍大多是一鳞半爪。求人不如求已。一狠心,自己看!这两天利用我们项目两期之间的一点空闲对j2se所支持的正则表达式来了个彻底研究!代价是……就是浪废了部门近十二张白纸。闲话少说,书归正传。
* 原理:
* 正则表达式的原理是有限状态自动机,自动机内部有有限个状态,有一个初始状态,有一个结束状态。自动机根据输入和自身内部的当前状态来决定下一步于什么。呵呵,这是很久以前学的东东了也记不清了,大家只作参照吧。
* java中的正则表达式:
* 从j2se1.4起java增加了对正则表达式的支持就是java.util.regex包,这个包中主要有3个类:pattern,代表模式,就是正则表达式自身,matcher,是一个有限状态自动机,其实大多数的活还是让pattern类于了,matcher往往只是简单的调用pattern,不知道这是什么模式。这两个类写的都很经典,还有不少算法在内值得有功力的人仔细研究一下。另一个是一个异常类当所用正则表达式不正确时抛出,是运行时异常。
* 几个难点:
* 1.line terminator
* line terminator 中文意终结符,是指一个或两个字符组成的字符序列。java中的
* 所有line terminator:
* a newline (line feed) character ('/n'),
* -----------换行符(0a)
* a carriage-return character followed immediately by a newline character ("/r/n"),
* -----------回车+换行(0d0a)
* a standalone carriage-return character ('/r'),
* -----------回车(0d)
* a next-line character ('/u0085'),
* ------------下一行符?(?表示我也不知道是什么,请大家明白的给我发mail
* a line-separator character ('/u2028'), or
* ------------行分隔符?
* a paragraph-separator character ('/u2029).
* ------------段落分隔符?
* if unix_lines mode is activated, then the only line terminators recognized are newline characters.
* 如果使用unix模式则只有/n被认为是line terminator,也就是在使用pattern时如下:
* pattern p=pattern.compile("正则表达式",pattern.unix_line);
* 或 pattern p=pattern.compile("(?d)正则表达式")
* "."匹配除line terminator以外的所有字符(未指定dotall时)
* 在指定dotall模式时"."匹配所有字符
* 2.quantifiers,greedy,reluctant and possessive.
* 这几个词不太好译,原文是greedy quantifiers,reluctant quantifiers and possessive quantifiers凭我这点英语我只好译作贪婪的量子,不情愿的量子和占有欲强的量子?这也太搞笑了好在我理解了他们的意思。这点等下我细说。
* 3. 对于[a-za-z],[a-d[h-i]],[^a-f],[b-f&&[a-z]],[b-f&&[^cd]]等形式的理解对于上述,原文用range,union,negation,intersection,subtraction等来描述range表是范围,union是并集,negation是取反,intersection是交集,subtraction是……是减法??反正是减去一部分的意思
* range a-z 从a到z的小写字母
* negation [^a-f]除了a-f之外所有的,全集是所有字符
* union [a-d[h-i]] a-d并h-i
* subtraction [b-f&&[^cd]] 就是b-f中除了cd以外的都是
* intersection[b-f&&[a-z]] 就是b-f与a-z中的公共部分
* 我总结了一下,其实就是方括号表示一个集合,集合中的元素用列举法表示如[abcd],但太多了怎么为?总不能把从a到z的全列举吧?那就用a-z表示且省略了方括号,交集用&&表示,并集省略,差集(对subtraction译成差集还差不多)用交集和取反来表示。所以,以上的可表示为:
* [[a-z][a-z]],[[a-d][h-i]],[^a-f],[[b-f]&&[a-z]],[[b-f]&&[^cd]]
* 这样是不是和我们的习惯相符了.
* 4.各个标志的意义
* 在生成pattern时可以同时使用几个标志来指定进行匹配时的方案。
* 用法形如:pattern p=pattern.compile(".*a?",pattern.unix_lines);
* 当同时指定多个标志时可以使用"|"操作符连接如:
* pattern p=pattern.compile(".*a?,pattern.unix_lines|pattern.dotall);
* 也可以在表达式中指定如:
* pattern p=pattern.compile("(?d).*a?");
* pattern p=pattern.compile("(?d)(?s).*a?");
* 以上两个定义和前面两个对应等价
* 所有的标志如下:
* constant equivalent embedded flag expression
pattern.canon_eq none enables canonical equivalence
pattern.case_insensitive (?i) enables case-insensitive matching
pattern.comments (?x) permits whitespace and comments in pattern.
pattern.multiline (?m) enables multiline mode.
pattern.doatall (?s) enables dotall mode
pattern.unicode_case (?u) enables unicode-aware case folding.
pattern.unix_lines (?d) enables unix lines mode
canon_eq 指定使用规范等价模式?这个我理解的也有限,是不是说只要指定了这个模式则ascii码的'a'就可以和unicode的'a'还有xxx码的'a'相等?请教各位。(mail to me)
case_insensitive 指定使用大小写不敏感的匹配模式,这个好理解,但要注意这个标志只是对ascii码有效,要使unicode在比较时也忽略大小写要同时指定unicode_case,就是要指定case_insensitive|unicode_case或使用(?i)(?u)
comments 指定使用注释和忽略空白,也就是".*a"==". *a #this is comments"我想这个在正则表达式很大,而且是在文件中输入时比较有用,平时我看也用不上。
multiline in multiline mode the expressions ^ and $ match just after
or just before, respectively, a line terminator or the end of the
input sequence. by default these expressions only match at the beginning
and the end of the entire input sequence
指定使用多行匹配模式,在默认模式下,^和$分别只匹配一个输入的开始和结束。
在这种模式下,^和$ 除了匹配整个输入的开始和结束外还匹配一个line terminator的后边和前边(不是前边和后边,就是说^匹配line terminator的后边$匹配line terminator的前边。
doatall 如指定了这个模式则"."可匹配任何字符包括line terminator unix_lines 指定这个模式时只有/n被认为是line terminator而/r和/r/n都不是其他的我一时想不起来了,在具体介绍时再说吧。
</p>
/
public class testreg2
{
public static void main(string[] args)
{
string str1 = "";
object str = "";
//注意:/r,/n,/b等转义字符在java字符串常量中要写成//r,//n,//b等,否则编译都过不去
///s匹配/r,/n,/r和空格
system.out.println("//s匹配//r,//n,//r和空格 "+" /t/n/r".matches("//s{4}"));
///s和/s互逆
system.out.println("//s和//s互逆 "+"/".matches("//s"));
//.不匹配/r和/n
system.out.println(".不匹配//r和//n "+"/r".matches("."));
system.out.println("/n".matches("."));
///w匹配字母,数字和下划线
system.out.println("//w匹配字母,数字和下划线 "+"a8_".matches("//w//w//w"));
///w和/w互逆
system.out.println("//w和//w互逆 "+"&_".matches("//w//w"));
///d匹配数字
system.out.println("//d匹配数字 "+"8".matches("//d"));
///d与/d互逆
system.out.println("//d与//d互逆"+"%".matches("//d"));
//两者都匹配但意文不同
system.out.println("======================");
system.out.println("表示//000a匹配//000a "+"/n".matches("/n"));
system.out.println("表示//n匹配换行 "+"/n".matches("//n"));
system.out.println("======================");
//两者都匹配但意文不同
system.out.println("/r".matches("/r"));
system.out.println("/r".matches("//r"));
system.out.println("======================");
//^匹配开头
system.out.println("^匹配开头"+"hell".matches("^hell"));
system.out.println("abc/nhell".matches("^hell"));
//$匹配结束
system.out.println("$匹配结束"+"my car/nabc".matches(".*ar$"));
system.out.println("my car".matches(".*ar$"));
闽公网安备 35060202000074号