ITeye博客

java 正则表达式学习

little_bill · 2010-09-30 14:16 · java&&jsp · 编程语言 · Java · Google · Go · 正则表达式 · XML · Markdown

[code="java"] import java.util.regex.*; public class Test{ public static void main(String args[]) throws Exception{ //基础 //String regex = "sales."; //String regex = ".a.."; //String regex = ".a.\\.xls"; //String regex = "[ns]a.\\.xls"; //String regex = "[ns]a[0123456789]\\.xls"; //String regex = "[ns]a[0-9]\\.xls";//同[0123456789]一样 String regex = "[ns]a[^0-9]\\.xls"; Test.go(regex, StrOne()); System.out.println("-----------example two------------------"); //匹配【】一个字符 regex = "[Rr]eg[Ee]x"; Test.go(regex, StrTwo()); System.out.println("-----------example three------------------"); //匹配 “+”一个或者多个 regex = "[\\w.]+@[\\w.]+\\.\\w+"; Test.go(regex, StrThree()); System.out.println("-----------example four------------------"); //匹配 “*”零个或者多个 regex = "\\w+[\\w.]*@[\\w.]+\\.\\w+"; Test.go(regex, StrThree()); System.out.println("-----------example five------------------"); //匹配 “?”零个或者一个 regex = "https?://[\\w./]+"; Test.go(regex, StrFour()); System.out.println("-----------example five------------------"); //匹配 “{2,4}”重复出现最少2个,最大4个 regex = "\\d{1,2}[-\\/]\\d{1,2}[-\\/]\\d{2,4}"; Test.go(regex, StrFive()); System.out.println("-----------example six------------------"); //匹配 “{2,4}”重复出现最少2个,最大4个,{3,}匹配至少3位,{3}匹配3位 regex = "\\d+:\\$\\d{3,}\\.\\d{2}"; Test.go(regex, StrSix()); System.out.println("-----------example seven-----贪婪型-------------"); /** * 贪婪型 懒惰型 * * *? * + +? * {n,} {n,}? */ regex = "<[Bb]>.*"; Test.go(regex, StrSeven()); System.out.println("-----------example seven-----懒惰型-------------"); regex = "<[Bb]>.*?"; Test.go(regex, StrSeven()); System.out.println("-----------example eight----\\b--------------"); //单词边界 \b regex = "\\bcat\\b"; Test.go(regex, StrEight()); System.out.println("-----------example eight----\\B--------------"); //不匹配单词边界 \B regex = "\\B-\\B"; Test.go(regex, StrEight()); System.out.println("-----------example nine-------错误-----------"); //字符串边界 ^字符串的开头(只有放在【】里边才表示非哦。。。) //字符串边界 $结束 regex = "<\\?xml.*\\?>"; Test.go(regex, StrNine()); System.out.println("-----------example nine-------正确(文件开始不是xml)------错误-----"); regex = "^\\s*<\\?xml.*\\?>"; Test.go(regex, StrNine()); System.out.println("-----------example nine-------正确-----------"); regex = "^\\s*<\\?xml.*\\?>"; Test.go(regex, StrNine_()); System.out.println("-----------example nine-------$---错误--------"); regex = "\\s*$"; Test.go(regex, StrNine__()); System.out.println("-----------example nine-------$---正确--------"); regex = "\\s*$"; Test.go(regex, StrNine___()); System.out.println("-----------example eleven---------------"); //字表达式 regex = "( ){2,}"; Test.go(regex, StrEleven()); System.out.println("-----------example twelve---------------"); //子表达式 regex = "\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}"; Test.go(regex, StrTwelve()); regex = "(\\d{1,3}.){3}\\d{1,3}"; Test.go(regex, StrTwelve()); System.out.println("-----------example thirteen---------------"); regex = "(19|20)\\d{2}"; Test.go(regex, StrThirteen()); System.out.println("-----------example IP---------------"); /** * 任何一个1位或2位的数字; * 任何一个以1位开头的3位数字; * 任何一个以2开头,第二位数字在0~4之间的3位数字; * 任何一个以25开头,第三位数字在0~5之间的3位数字; */ regex = "(((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|(25[0-5]))\\.){3}((\\d{1,2})|(1\\d{2})|(2[0-4]\\d)|(5[0-5]))"; Test.go(regex, StrTwelve()); System.out.println("-----------example fourteen---回溯引用------------"); //回溯引用,查出重复的 /** * [ ]匹配一个或者多个空格; * \w+匹配一个或多个字母数字字符; * (\w+)是一个子表达式 * 当(\w+)匹配到单词of的时候,\1也匹配单词of * 回溯引用指的是后半部分引用在前半部分中定义的字表达式 * \1表示模式里第一个表达式 */ regex = "[ ]+(\\w+)[ ]+\\1"; Test.go(regex, StrFourteen()); System.out.println("-----------example Fiveteen-----回溯引用----------"); regex = "<[hH]([1-6])>.*?"; Test.go(regex, StrFiveteen()); System.out.println("-----------example 搜索》替换 给邮件加链接-----回溯引用----------"); regex = "[\\w.]+@[\\w.]+\\.\\w+"; Test.go(regex, StrThree()); regex = StrThree().replaceAll("(\\w+[\\w\\.]*@[\\w\\.]+\\.\\w+)", "$1"); System.out.println(regex); System.out.println("-----------example -----向前查找---------"); //向前查找忽略":" regex = ".+(?=:)"; Test.go(regex, StrSeventeen()); regex = ".+(:)"; Test.go(regex, StrSeventeen()); System.out.println("-----------example -----向后查找---------"); //向后查找 regex = "(?<=\\$)[0-9.]+"; Test.go(regex, StrEighteen()); System.out.println("-----------example -----向前向后查找---------"); //向前向后查找 regex = "(?<=<[tT][iI][tT][lL][eE]>).*(?=)"; Test.go(regex, StrNineteen()); } public static String StrNineteen(){ String str = "" + "Hello World" + ""; return str; } public static String StrEighteen(){ String str = "ABCD1:$2.45\n" + "YHTG8:$56.65"; return str; } public static String StrSeventeen(){ String str = "http://www.google.com/\n" + "https://www.sina.com.cn/\n" + "htpps://www.wiki.com"; return str; } public static String StrSixteen(){ String str = "313-555-1234\n" + "248-555-9999\n" + "810-555-9000"; return str; } public static String StrFiveteen(){ String str = "" + "

Welcome

" + "Hello world hi" + "

Welcome

" + "Hello world hii" + "

Welcome

" + ""; return str; } public static String StrFourteen(){ String str = "This is a block of of text,here are are,they and and"; return str; } public static String StrThirteen(){ String str = "ID:0123" + "SEX:M" + "DOB:1967-08-17" + "Status:Avtive"; return str; } public static String StrTwelve(){ String str = "Ping hog. forta.com [12.159.46.200]"; return str; } public static String StrEleven(){ String str = "Hello,my name Windows  2000,"; return str; } public static String StrNine___(){ String str = "" + ""; return str; } public static String StrNine__(){ String str = "" + " 1234;"; return str; } public static String StrNine_(){ String str = ""; return str; } public static String StrNine(){ String str = "This is bad, real bad!\n" + ""; return str; } public static String StrEight(){ String str = "The cat cattered his food all over the room.\n" + "appears on your color - coded pass-key"; return str; } public static String StrSeven(){ String str = "living in AK and HI"; return str; } public static String StrSix(){ String str = "1001:$496.80\n" + "1002:$26.43\n" + "1003:$4125.85\n" + "1004:$517.98"; return str; } public static String StrFive(){ String str = "4/8/03\n" + "10-6-2004\n" + "2/2/2\n" + "01-01-01"; return str; } public static String StrFour(){ String str = "The URL is http://www.forta.com/,to connect\n" + "securely use https://www.forta.com/ insteads\n" + "securely use httpssssssss://www.forta.com/"; return str; } public static String StrThree(){ String str = "..ben.forta@forta.com\n" + "ben@urgent.forta.com\n" + "Hello .ben@fortaben.com is my email addres."; return str; } public static String StrTwo(){ String str = "The phrase 'regular expression' is often\n" + "abbreviated as RegEx or regex."; return str; } public static String StrOne(){ String str = "sales1.xls\n" + "orders3.xls\n"+ "sales2.xls\n" + "sales3.xls\n" + "apac1.xls\n" + "eroupe2.xls\n" + "na1.xls\n" + "na2.xls\n" + "sa1.xls\n" + "ca1.xls\n" + "na58xls\n" + "sam.xls"; return str; } public static void go(String regex,String str){ Pattern p = Pattern.compile(regex); Matcher m = p.matcher(StrOne()); p = Pattern.compile(regex); m = p.matcher(str); String val = null; while (m.find()){ val = m.group(); System.out.println("MATCH: " + val); } } } [/code]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。