分解字母和数字的几个正则 import java.util.regex.Matcher; import java.util.regex.Pattern; public class 分解字母和数字 ... { public static void main(String[] args) ...{ // test1(); // test2(); // test3(); // test4(); // test5(); // test6(); // test7(); // test8(); // test9(); // test10(); // test11(); // test12(); // test13();// test14(); test15(); } public static void test1() ...{ String str = "E89.4IOUI98.790F78S.978jiodau8.fd90s8"; // String str="4.50XFLAX4.5"; // String str="120.00YR"; String ptn = "((?<=[a-zA-Z])(?=[0-9|/.]))|((?=[a-zA-Z])(?<=[0-9|/.]))"; String[] s = str.split(ptn); // String[] s=str.split("((?<=[0-9])(?=[a-zA-Z]))|((?=[0-9])(?<=[a-zA-Z]))"); for (int i = 0; i < s.length; i++) ...{ System.out.println(s[i]); } System.out.println("..................................."); } public static void test2() ...{ String str = "<root id= 'tt ' > <![CDATA[111 3333]] > <root >" + " <basisInfo > <![CDATA[222 4444]] > 1"; Pattern p = Pattern.compile("(.*?)(</!/[CDATA.*?/] >)"); Matcher m = p.matcher(str); String rtn = ""; String group = ""; while (m.find()) ...{ System.out.println("m.group(1):" + m.group(1) + " m.group(2):" + m.group(2)); rtn += m.group(1) + m.group(2).replaceAll("/s", " "); group = m.group(2); } rtn += str.substring(str.lastIndexOf(group) + group.length()); System.out.println(rtn); } public static void test3() ...{ String reg = "北京|上海|南京"; Pattern p = Pattern.compile(reg); Matcher m = p.matcher("北京和南京"); while (m.find()) ...{ System.out.println(m.group()); } } public static void test4() ...{ Pattern p = Pattern.compile("ET.+?(?=[+/-/*//),])"); String str = "str(ET0.a)+ET1.b+substring(ET2.C,1,2)"; Matcher m = p.matcher(str); while (m.find()) ...{ System.out.println(m.group()); } } public static void test5() ...{ String str = "head header ahead "head""; System.out.println(str.replaceAll("/bhead/b", "aaaa")); } public static void test6() ...{ Pattern p = Pattern.compile("(/d*)([A-Z]*),?"); String str = "4523425432NB,32423TY,4NT,43232423432YT"; Matcher m = p.matcher(str); while (m.find()) ...{ System.out.println(m.group(1) + " " + m.group(2)); } } public static void test7() ...{ String str = "4523425432NB,32423TY,4NT,43232423432YT"; String ptn = "((?<=[a-zA-Z]),?(?=[0-9]))|((?=[a-zA-Z]),?(?<=[0-9]))"; String[] s = str.split(ptn); for (int i = 0; i < s.length; i++) ...{ System.out.println(s[i]); } } public static void test8() ...{ String strs[] = ...{ "ababba", "bbababbaaabbababb", "bbababbaaabbbababb", "cbababbaaabbbabaaab" }; String ptn = "/b((?!bbb)[ab])+/b"; for (int i = 0; i < strs.length; i++) ...{ Pattern p = Pattern.compile(ptn); Matcher m = p.matcher(strs[i]); if (m.find()) ...{ System.out.println("i:" + i + " " + strs[i]); } } } public static void test9() ...{ String str = "lksafaksdkjfjkasjd[em20333]lskjakfjkjal;jd"; str = str.replaceAll("/[(em/d+)/]", " <img src= "$1.gif " >"); System.out.println(str); str = " <tr > <td > <a href=/ >07/16 </a > </td > <td >07/16 </td > <td >07/16 </td > <td >07/16 </td > <td >07/16 </td > <td >07/16 </td > "; str = str.replaceAll("(?<=/d+)/", "-"); System.out.println(str); } public static void test10() ...{ Pattern p = Pattern.compile("(?<=&q=).+?(?=&)"); String str = "&q=aaa&bbb&ccc&"; Matcher m = p.matcher(str); while (m.find()) ...{ System.out.println(m.group()); } } public static void test11() ...{ String str[] = new String[] ...{ "abcd", "123", "adc", "1abc" }; Pattern p = Pattern.compile("(?!abc)^(.*)$(?<!abc)"); // Pattern p = Pattern.compile("((?!abc)/w)+"); for (int i = 0; i < str.length; i++) ...{ Matcher m = p.matcher(str[i]); if (m.find()) ...{ System.out.println(m.group(1)); } } } public static void test12() ...{ String s = "TITLE承运日期'TITLE 'TITLE '承运日期''"; // 假设s = TITLE承运日期 Pattern p = Pattern.compile("(?<=TITLE)(?:.*?)([一-龥]+)"); Matcher m = p.matcher(s); while (m.find()) ...{ // 判断是否匹配 System.out.println(m.group(1)); // 匹配成功,输出该中文字符 } } public static void test13() ...{ // 日期测试...不过....不对.... String ptn = "^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1/d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((01,3-9])|(1[0-2]))-(29|30)))))$"; String str[] = ...{ "2007-02-28", "2008-02-29", "2007-04-29" }; Pattern p = Pattern.compile(ptn); for (int i = 0; i < str.length; i++) ...{ Matcher m = p.matcher(str[i]); if (m.find()) ...{ System.out.println("Date:" + m.group()); } } } public static void test14() ...{ String str2 = "+8613688395123/TYPE=PLMN,+8613688395124/TYPE=PLMN@mmsc-cd2-rsv.monternet.com "; // String str= "+8613688395123,+8613688395124 "; String ptn = "(/+/d*).*(,/+/d*)"; Pattern p = Pattern.compile(ptn); Matcher m = p.matcher(str2); if (m.find()) ...{ System.out.println("str:" + m.group(1)+m.group(2)); } } public static void test15() ...{// String str2 = "INDEX A ( B,C )"; String str2 = "INDEX ( D,E ) "; // String str= "+8613688395123,+8613688395124 "; String ptn = "INDEX/s+[A-Za-z]?/s+/(/s+[A-Za-z],[A-Za-z]/s+/)"; Pattern p = Pattern.compile(ptn); Matcher m = p.matcher(str2); if (m.find()) ...{ System.out.println("str:" + m.group()); } }}