日期:2014-05-20 浏览次数:20759 次
public class Test981 { public static void reflect(String... str){ Pattern p1 = Pattern.compile("\\[\\d+:\\d+\\]"); Pattern p2 = Pattern.compile("JF?K?\\d+"); for (String string : str) { Matcher m1 = p1.matcher(string); Matcher m2 = p2.matcher(string); if(m1.find()) System.out.println(m1.group().replaceAll("[\\[\\]]", "")); if(m2.find()) System.out.println(m2.group()); } } public static void main(String[] args) { String s1 = "[21231853:680492]smpp_TAQ:_13189867588_1_aaaaaaaa_J2012052123360846583"; String s2 = "[21231853:861244]smpp_TUQ:_JFK20120521510548945_13249852861_1_aaaaaaaa_3000_0_N_2012-05-21 23:36:08_511201167443476"; reflect(s1 , s2); } }
------解决方案--------------------
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Wei5445 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub regexStr(); } public static void regexStr(){ String str1 = "[21231853:680492]smpp_TAQ:_13189867588_1_aaaaaaaa_J2012052123360846583", str2 = "[21231853:861244]smpp_TUQ:_JFK20120521510548945_13249852861_1_aaaaaaaa_3000_0_N_2012-05-21 23:36:08_511201167443476"; String regex = "\\[(\\d+:\\d+)\\]smpp_T[AU]{1}Q:.*?_([a-zA-Z]+\\d+).*?"; Matcher m = Pattern.compile(regex).matcher(str1); while(m.find()){ System.out.println(m.group(1)); System.out.println(m.group(2)); } } } 运行: 21231853:680492 J2012052123360846583
------解决方案--------------------
public static void main(String[] args) { String s = "I.[21231853:680492]smpp_TAQ:_13189867588_1_aaaaaaaa_J2012052123360846583"; // String s = "I.[21231853:861244]smpp_TUQ:_JFK20120521510548945_13249852861_1_aaaaaaaa_3000_0_N_2012-05-21 23:36:08_511201167443476"; Matcher m = Pattern.compile("\\[(.+?)\\].+?_(J[^_]+)(_.+)?").matcher(s); while(m.find()){ System.out.println(m.group(1)); System.out.println(m.group(2)); } }
------解决方案--------------------
for example
String[] arr = {"I.[21231853:680492]smpp_TAQ:_13189867588_1_aaaaaaaa_J2012052123360846583", "I.[21231853:861244]smpp_TUQ:_JFK20120521510548945_13249852861_1_aaaaaaaa_3000_0_N_2012-05-21 23:36:08_511201167443476"}; for (String s : arr) { //替换后分割,sa[0],sa[1]就是LZ所需要的 String[] sa = s.replaceAll(".*?\\[(\\d+:\\d+)\\].*?(J[A-Z]*\\d+).*", "$1,$2").split(","); System.out.printf("first=%s, second=%s\n", sa[0], sa[1]); }