当前位置: 代码迷 >> J2SE >> 请问个正则表达式有关问题
  详细解决方案

请问个正则表达式有关问题

热度:68   发布时间:2016-04-24 00:57:03.0
请教个正则表达式问题
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

想用一个正则表达式都可以匹配这两种字符串 
对于第一字符串,取21231853:680492和J2012052123360846583
 
对于第二字符串,取21231853:861244和JFK20120521510548945 


------解决方案--------------------
Java code
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);    }}
------解决方案--------------------
Java code
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:680492J2012052123360846583
------解决方案--------------------
Java code
    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
Java code
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]);}