java可用正则表达式
首先看官方提供的正则表达式框架测试类:
package gnu.regexp.util; import gnu.regexp.*; import java.io.StringBufferInputStream; import java.io.StringReader; /** * This is a very basic testsuite application for gnu.regexp. * * @author <A HREF="mailto:[email protected]">Wes Biggs</A> * @version 1.1.1 */ public class Tests { private Tests() { } private static void check(RE expr, String input, String expect, int id) { // Test it using all possible input types check(expr.getMatch(input),expect,id, "String"); check(expr.getMatch(new StringBuffer(input)),expect,id, "StringBuffer"); check(expr.getMatch(input.toCharArray()),expect,id, "char[]"); check(expr.getMatch(new StringReader(input)),expect,id, "Reader"); check(expr.getMatch(new StringBufferInputStream(input)),expect,id, "InputStream"); } private static void check(REMatch m, String expect, int x, String type) { if ((m == null) || !m.toString().equals(expect)) System.out.print("*** Failed"); else System.out.print("Passed"); System.out.println(" test #"+x + " (" + type + ")"); } /** * Runs the testsuite. No command line arguments are necessary. * * @exception REException An error occurred compiling a regular expression. */ public static void main(String[] argv) throws REException { RE e; e = new RE("(.*)z"); check(e,("xxz"),"xxz",1); e = new RE(".*z"); check(e,("xxz"),"xxz",2); e = new RE("(x|xy)z"); check(e,("xz"),"xz",3); check(e,("xyz"),"xyz",4); e = new RE("(x)+z"); check(e,("xxz"),"xxz",5); e = new RE("abc"); check(e,("xyzabcdef"),"abc",6); e = new RE("^start.*end$"); check(e,("start here and go to the end"),"start here and go to the end",7); e = new RE("(x|xy)+z"); check(e,("xxyz"),"xxyz",8); e = new RE("type=([^ \t]+)[ \t]+exts=([^ \t\n\r]+)"); check(e,("type=text/html exts=htm,html"),"type=text/html exts=htm,html",9); e = new RE("(x)\\1"); check(e,("zxxz"),"xx", 10); e = new RE("(x*)(y)\\2\\1"); check(e,("xxxyyxx"),"xxyyxx",11); e = new RE("[-go]+"); check(e,("go-go"),"go-go",12); e = new RE("[\\w-]+"); check(e,("go-go"),"go-go",13); e = new RE("^start.*?end"); check(e,("start here and end in the middle, not the very end"),"start here and end",14); e = new RE("\\d\\s\\w\\n\\r"); check(e,(" 9\tX\n\r "),"9\tX\n\r",15); e = new RE("zow",RE.REG_ICASE); check(e,("ZoW"),"ZoW",16); e = new RE("(\\d+)\\D*(\\d+)\\D*(\\d)+"); check(e,("size--10 by 20 by 30 feet"),"10 by 20 by 30",17); e = new RE("(ab)(.*?)(d)"); REMatch m = e.getMatch("abcd"); check(m,"abcd",18, "String"); System.out.println(((m.toString(2).equals("c")) ? "Pass" : "*** Fail") + "ed test #19"); e = new RE("^$"); check(e,(""),"",20); e = new RE("a*"); check(e,(""),"",21); check(e,("a"),"a",22); check(e,("aa"),"aa",23); e = new RE("(([12]))?"); check(e,("12"),"1",24); e = new RE("(.*)?b"); check(e,("ab"),"ab",25); e = new RE("(.*)?-(.*)"); check(e,("a-b"), "a-b", 26); e = new RE("(a)b"); check(e,("aab"), "ab", 27); e = new RE("[M]iss"); check(e,("one Mississippi"), "Miss", 28); e = new RE("\\S Miss"); check(e,("one Mississippi"), "e Miss", 29); e = new RE("a*"); check(e,("b"),"",30); check(e,("ab"),"a",31); check(e,("aab"),"aa",32); // Single character should match anywhere in String e = new RE("a"); check(e,("a"),"a", 33); check(e,("ab"),"a", 34); check(e,("ba"),"a", 35); check(e,("bab"),"a", 36); e = new RE("(?!.*12)(?=.*1)(.*)");//包含1且不包含12 check(e,("12"),"2", 37); System.err.println(e.isMatch("132")); check(e,("1"),"", 38); } }
java自带正则表达式还无法完全支持正则标准,要很好控制定向搜索,正则表达式还是少不了的。^_^
http://www.baidu.com/s?wd=gnu%20regex&pn=0&tn=baiduhome_pg&ie=utf-8&usm=1
相关推荐
wangzhaotongalex 2020-10-20
wyq 2020-11-11
TLROJE 2020-10-26
风雨断肠人 2020-10-13
duanqingfeng 2020-09-29
rechanel 2020-11-16
cshanzhizi 2020-10-16
luofuIT成长记录 2020-09-22
phphub 2020-09-10
taomengxing 2020-09-07
MaggieRose 2020-08-19
flyingssky 2020-08-18
山水沐光 2020-08-18
jyj00 2020-08-15
AHuqihua 2020-08-09
山水沐光 2020-08-03