日期:2014-05-17  浏览次数:20825 次

帮忙写这个正则
{K:"808233617844421474",N:"徐向荣",DN:"经营班子",A:"-4732166176755790536",Y:"huafa_xxr@163.com",M:"13809239771"},

{K:"2215866499036760057",N:"郑旭斌",DN:"经营班子",A:"-8037408714807164460",Y:"bungigi@yahoo.com.cn",M:"13926920160"},

{K:"-5181729055305085065",N:"郭云飞",DN:"高管",A:"-5652342830334391775"},

{K:"-2030471991893342160",N:"阳静",DN:"高管",A:"-5652342830334391775",Y:"yj@cnhuafas.com",M:"13005766577"},

{K:"-967229778938475655",N:"文仕钊",DN:"高管",A:"7264905588660127884",M:"13902869308"}]},

{K:"-7164715885856863925",N:"收发员组",D:"5725175934914479521",T:2,L:["-4534144519453586850"],M:["8694035076497230978","3371638072285806829","6305501376912705488","-7170534215949793268","-3538499796194538423","-2571637927348909194","-2432972338430653801","4816301693824812020","3857284423883704135","-9033607757720829481","-620805614765978609","-2702676425859773758"],
E:[
{K:"-4534144519453586850",N:"田涛",DN:"信息技术管理部",A:"-5652342830334391775",M:"13825650122"},

{K:"8694035076497230978",N:"贺娜",DN:"成本管理部",A:"-5652342830334391775"},


我现在要取到里面的前几项的值,例如第一个

{K:"808233617844421474",N:"徐向荣",DN:"经营班子",A:"-4732166176755790536",Y:"huafa_xxr@163.com",M:"13809239771"},

我想要拿到 808233617844421474,徐向荣,经营班子,:"-4732166176755790536 这4个值,其他的一样只要前面4个,但里面有个"收发员组" 那条记录我是不要的,因为里面都没有DN

------解决方案--------------------
这不是json对象么?你自己想要什么就要什么啊。
------解决方案--------------------
.*K:"(.*?)",N:"(.*?)",DN:"(.*?)",A:"(.*?)".*

这个是 emedit用正则写的表达式 这样的结果是不包括 收发员组
------解决方案--------------------
Java code


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    public static void main(String[] args) {
        Pattern p = Pattern.compile("(\\w*:\\\")(.*)(\")") ;
        String str = "{K:\"808233617844421474\",N:\"徐向荣\",DN:\"经营班子\",A:\"-4732166176755790536\",Y:\"huafa_xxr@163.com\",M:\"13809239771\"}";
        String[] strs = str.split(",");
        for(int i=0;i<strs.length&&i<4;i++) {
            Matcher m = p.matcher(strs[i]);
            if(m.find()) {
                System.out.println(m.group(2));
            }
            
        }
        
        
    }    
}

------解决方案--------------------
Java code

public class Test {
    public static void main(String[] args) {
        String reg = "\"";
        String str[] = {"K:\"808233617844421474\"","N:\"徐向荣\"","DN:\"经营班子\"","A:\"-4732166176755790536\"","Y:\"huafa_xxr@163.com\"","M:\"13809239771\""};
        
        for(int i=0;i<str.length&&i<4;i++) {
            
            System.out.println(str[i].split(reg)[1]);
        }

        
        
    }    
}