日期:2014-05-19  浏览次数:20830 次

java 正则匹配第一个结果,其余丢弃?????
这是java的正则:
Java code

 Pattern pattern01=Pattern.compile("(dataIndex.*:((\'(.*?)\')||(\"(.*?)\"))[,]?)");
        Matcher matcher01=pattern01.matcher(line);

        if(matcher01.find()){
            System.out.println(matcher01.group());
        }


以下是匹配的结果:
JScript code

dataIndex:"name",id:"name",locked:
dataIndex:"sex",id:"sex",renderer:sexChange,locked:
dataIndex:"age",id:"age",sortable:true,renderer:ageChange,locked:
dataIndex:"patient_no",id:"patient_no",locked:
dataIndex:"cardid",id:"cardid",locked:
dataIndex:"coopmedcode",id:"coopmedcode",locked:
dataIndex:"freetype_name",id:
dataIndex:"casehistory_no",id:
dataIndex:"in_order",id:"in_order",align:
dataIndex:"birthday",id:"birthday",align:"center",renderer:
dataIndex:"marragename",id:"marragename",align:
dataIndex:"jobname",id:"jobname",align:
dataIndex:'nation'
dataIndex:'nativename'
dataIndex:'id_card'
dataIndex:'address2'
dataIndex:'in_day_date',renderer:
dataIndex:'out_day_date',align:'center',renderer:
dataIndex:'at_hosptial_day_amount'
dataIndex:'in_office_code'
dataIndex:'in_ward_code'
dataIndex:'curr_office_code'
dataIndex:'curr_ward_code'
dataIndex:'out_office_code'
dataIndex:'out_ward_code'
dataIndex:'change_office_code'
dataIndex:'mz_icd10name'
dataIndex:'style_in',renderer:
dataIndex:'ry_icd10name'
dataIndex:'diagnose_date',align:'center'
dataIndex:'mostly_icd10'
dataIndex:'mostly_sicd10'
dataIndex:'out_status',renderer:
dataIndex:'office_director_code'
dataIndex:'director_doctor_code'
dataIndex:'leading_doctor_code'
dataIndex:'hospitalzation_doctor_code'
dataIndex:'sum_fee'
dataIndex:'sum_must_fee'
dataIndex:'sum_other_fee'
dataIndex:'curr_fee'
dataIndex:'curr_must_fee'
dataIndex:'curr_other_fee'
dataIndex:'in_type_name'
dataIndex:'is_other_therapy',renderer:
dataIndex:'out_type',renderer:
dataIndex:'therapy_type_name'
dataIndex:'mz_ccd_bz'
dataIndex:'mz_ccd_zh'
dataIndex:'mz_doctor_code_chinese'
dataIndex:'ry_ccd_bz'
dataIndex:'ry_ccd_zh'
dataIndex:'mostly_ccd_bz'
dataIndex:'mostly_ccd_zh'
dataIndex:'out_status_chinese',renderer:
dataIndex:'out_flag',renderer:
dataIndex:'payowe_fee'


想要的结果:
dataIndex:"name",id:"name",locked: 例如这个变成这样dataIndex:"name",
dataIndex:"jobname",id:"jobname",align:例如这个变成这样dataIndex:"jobname",
以下的都是满足的.

------解决方案--------------------
没必要这么复杂吧?

你试试这个,直接给你搞定:
String line = "dataIndex:\"sex\",id:\"sex\",renderer:sexChange,locked:";
System.out.println(line.replaceAll("([^,]*)[^$]*", "$1"));

输出:
dataIndex:"sex"