正则在开发过程中的妙用

1.大量字段的sql语句

去掉``

INSERT INTO `ttreport`.`tb_tt_operation` (`province`, `city`, `area`, `nation_area`, `country`, `site_location`, `code`, `id`, `longitude`, `latitude`, `maintain_state`, `status`, `fsu_time`, `location`, `site_location_source`, `operator_share_status`, `business_scene`, `mobile_site_type`, `mobile_site_maintain_object`, `mobile_equipment_system`, `mobile_remote_site_name`, `if_mobile_remote_site_online_handle`, `mobile_internal_site_level`, `mobile_accept_no_responsibility_time`, `mobile_if_buy_electricity_service`, `unicorn_site_type`, `unicorn_site_maintain_object`, `unicorn_equipment_system`, `unicorn_remote_site_name`, `if_unicorn_remote_site_online_handle`, `unicorn_internal_site_level`, `unicorn_accept_no_responsibility_time`, `unicorn_if_buy_electricity_service`, `telecom_site_type`, `telecom_site_maintain_object`, `telecom_equipment_system`, `telecom_remote_site_name`, `if_telecom_remote_site_online_handle`, `telecom_internal_site_level`, `telecom_accept_no_responsibility_time`, `telecom_if_buy_electricity_service`, `administrative_areas`, `area_manager`, `area_manager_phone`, `computerroom_maintain_personnel`, `irontower_maintain_personnel`, `irontower_maintain_personnel_phone`, `operator_name`, `mobile_maintain_personnel`, `unicorn_maintain_personnel`, `telecom_maintain_personnel`, `property_type`, `climb_site_complexity`, `climb_site_personnel`, `climb_site_phone`, `if_0_6_climb_site`, `climb_site_complexity_prototype`, `power_supply_mode_first`, `power_supply_mode_second`, `electricity_meter_location`, `subordinative_electricity_public_change_name`, `subordinative_electricity_line_name`, `power_supply_phone`, `if_computerrome_handle`, `if_install_operator_equipment`, `property_right_type`, `if_possess_moving_ring_monitors_installation_conditions`, `fsu_coporation`, `if_possess_electricity_condition`, `stagnation_climb_site_time`, `if_0_6_climb_site_generate_electricity`, `electricity_need_power`, `accumulator_basic_gurantee_time`, `accumulator_extra_gurantee_time`, `generation_voltage_threshold`, `accumulator_duration_time`, `electricity_wait_time`, `electricity_interface_location`, `fixation_oil_engine`, `total_load`, `cause_no_electricity`, `extra_cause_no_electricity`, `if_exemption_site`, `cause_site_exemption_maStringain_environment`, `night_exemption_site`, `night_exemption_time`, `supplier_name`, `subject_number`, `geographical_factor`, `special_area_type`, `mobile_share_ratio`, `unicorn_share_ratio`, `telecom_share_ratio`, `install_multi_fsu`, `site_type`, `site_code`, `check_handle_date`, `mobile_site_rent_time`, `unicorn_site_rent_time`, `telecom_site_rent_time`, `oil_engine_electricity_fee_type`, `fsu_build_method`, `backup_power_group`, `room_site_rru_number`, `client_outside_industry`, `room_site_scale`, `onetime_electricity_time`, `site_gurantee_level`, `remark`)

把``替换掉格式化

INSERT INTO `ttreport`.`tb_tt_operation` (province, city, area, nation_area, country, site_location, code, id, longitude, latitude, maintain_state, status, fsu_time, location, site_location_source, operator_share_status, business_scene, mobile_site_type, mobile_site_maintain_object, mobile_equipment_system, mobile_remote_site_name, if_mobile_remote_site_online_handle, mobile_internal_site_level, mobile_accept_no_responsibility_time, mobile_if_buy_electricity_service, unicorn_site_type, unicorn_site_maintain_object, unicorn_equipment_system, unicorn_remote_site_name, if_unicorn_remote_site_online_handle, unicorn_internal_site_level, unicorn_accept_no_responsibility_time, unicorn_if_buy_electricity_service, telecom_site_type, telecom_site_maintain_object, telecom_equipment_system, telecom_remote_site_name, if_telecom_remote_site_online_handle, telecom_internal_site_level, telecom_accept_no_responsibility_time, telecom_if_buy_electricity_service, administrative_areas, area_manager, area_manager_phone, computerroom_maintain_personnel, irontower_maintain_personnel, irontower_maintain_personnel_phone, operator_name, mobile_maintain_personnel, unicorn_maintain_personnel, telecom_maintain_personnel, property_type, climb_site_complexity, climb_site_personnel, climb_site_phone, if_0_6_climb_site, climb_site_complexity_prototype, power_supply_mode_first, power_supply_mode_second, electricity_meter_location, subordinative_electricity_public_change_name, subordinative_electricity_line_name, power_supply_phone, if_computerrome_handle, if_install_operator_equipment, property_right_type, if_possess_moving_ring_monitors_installation_conditions, fsu_coporation, if_possess_electricity_condition, stagnation_climb_site_time, if_0_6_climb_site_generate_electricity, electricity_need_power, accumulator_basic_gurantee_time, accumulator_extra_gurantee_time, generation_voltage_threshold, accumulator_duration_time, electricity_wait_time, electricity_interface_location, fixation_oil_engine, total_load, cause_no_electricity, extra_cause_no_electricity, if_exemption_site, cause_site_exemption_maStringain_environment, night_exemption_site, night_exemption_time, supplier_name, subject_number, geographical_factor, special_area_type, mobile_share_ratio, unicorn_share_ratio, telecom_share_ratio, install_multi_fsu, site_type, site_code, check_handle_date, mobile_site_rent_time, unicorn_site_rent_time, telecom_site_rent_time, oil_engine_electricity_fee_type, fsu_build_method, backup_power_group, room_site_rru_number, client_outside_industry, room_site_scale, onetime_electricity_time, site_gurantee_level, `remark`)

 把所有的字段前面加上":"

province, city, area, nation_area, country, site_location, code, id, longitude, latitude, maintain_state, status, fsu_time, location, site_location_source, operator_share_status, business_scene, mobile_site_type, mobile_site_maintain_object, mobile_equipment_system, mobile_remote_site_name, if_mobile_remote_site_online_handle, mobile_internal_site_level, mobile_accept_no_responsibility_time, mobile_if_buy_electricity_service, unicorn_site_type, unicorn_site_maintain_object, unicorn_equipment_system, unicorn_remote_site_name, if_unicorn_remote_site_online_handle, unicorn_internal_site_level, unicorn_accept_no_responsibility_time, unicorn_if_buy_electricity_service, telecom_site_type, telecom_site_maintain_object, telecom_equipment_system, telecom_remote_site_name, if_telecom_remote_site_online_handle, telecom_internal_site_level, telecom_accept_no_responsibility_time, telecom_if_buy_electricity_service, administrative_areas, area_manager, area_manager_phone, computerroom_maintain_personnel, irontower_maintain_personnel, irontower_maintain_personnel_phone, operator_name, mobile_maintain_personnel, unicorn_maintain_personnel, telecom_maintain_personnel, property_type, climb_site_complexity, climb_site_personnel, climb_site_phone, if_0_6_climb_site, climb_site_complexity_prototype, power_supply_mode_first, power_supply_mode_second, electricity_meter_location, subordinative_electricity_public_change_name, subordinative_electricity_line_name, power_supply_phone, if_computerrome_handle, if_install_operator_equipment, property_right_type, if_possess_moving_ring_monitors_installation_conditions, fsu_coporation, if_possess_electricity_condition, stagnation_climb_site_time, if_0_6_climb_site_generate_electricity, electricity_need_power, accumulator_basic_gurantee_time, accumulator_extra_gurantee_time, generation_voltage_threshold, accumulator_duration_time, electricity_wait_time, electricity_interface_location, fixation_oil_engine, total_load, cause_no_electricity, extra_cause_no_electricity, if_exemption_site, cause_site_exemption_maStringain_environment, night_exemption_site, night_exemption_time, supplier_name, subject_number, geographical_factor, special_area_type, mobile_share_ratio, unicorn_share_ratio, telecom_share_ratio, install_multi_fsu, site_type, site_code, check_handle_date, mobile_site_rent_time, unicorn_site_rent_time, telecom_site_rent_time, oil_engine_electricity_fee_type, fsu_build_method, backup_power_group, room_site_rru_number, client_outside_industry, room_site_scale, onetime_electricity_time, site_gurantee_level, remark

 编写正则替换之

替换结果为

:province, :city, :area, :nation_area, :country, :site_location, :code, :id, :longitude, :latitude, :maintain_state, :status, :fsu_time, :location, :site_location_source, :operator_share_status, :business_scene, :mobile_site_type, :mobile_site_maintain_object, :mobile_equipment_system, :mobile_remote_site_name, :if_mobile_remote_site_online_handle, :mobile_internal_site_level, :mobile_accept_no_responsibility_time, :mobile_if_buy_electricity_service, :unicorn_site_type, :unicorn_site_maintain_object, :unicorn_equipment_system, :unicorn_remote_site_name, :if_unicorn_remote_site_online_handle, :unicorn_internal_site_level, :unicorn_accept_no_responsibility_time, :unicorn_if_buy_electricity_service, :telecom_site_type, :telecom_site_maintain_object, :telecom_equipment_system, :telecom_remote_site_name, :if_telecom_remote_site_online_handle, :telecom_internal_site_level, :telecom_accept_no_responsibility_time, :telecom_if_buy_electricity_service, :administrative_areas, :area_manager, :area_manager_phone, :computerroom_maintain_personnel, :irontower_maintain_personnel, :irontower_maintain_personnel_phone, :operator_name, :mobile_maintain_personnel, :unicorn_maintain_personnel, :telecom_maintain_personnel, :property_type, :climb_site_complexity, :climb_site_personnel, :climb_site_phone, :if_0_6_climb_site, :climb_site_complexity_prototype, :power_supply_mode_first, :power_supply_mode_second, :electricity_meter_location, :subordinative_electricity_public_change_name, :subordinative_electricity_line_name, :power_supply_phone, :if_computerrome_handle, :if_install_operator_equipment, :property_right_type, :if_possess_moving_ring_monitors_installation_conditions, :fsu_coporation, :if_possess_electricity_condition, :stagnation_climb_site_time, :if_0_6_climb_site_generate_electricity, :electricity_need_power, :accumulator_basic_gurantee_time, :accumulator_extra_gurantee_time, :generation_voltage_threshold, :accumulator_duration_time, :electricity_wait_time, :electricity_interface_location, :fixation_oil_engine, :total_load, :cause_no_electricity, :extra_cause_no_electricity, :if_exemption_site, :cause_site_exemption_maStringain_environment, :night_exemption_site, :night_exemption_time, :supplier_name, :subject_number, :geographical_factor, :special_area_type, :mobile_share_ratio, :unicorn_share_ratio, :telecom_share_ratio, :install_multi_fsu, :site_type, :site_code, :check_handle_date, :mobile_site_rent_time, :unicorn_site_rent_time, :telecom_site_rent_time, :oil_engine_electricity_fee_type, :fsu_build_method, :backup_power_group, :room_site_rru_number, :client_outside_industry, :room_site_scale, :onetime_electricity_time, :site_gurantee_level, remark

2.爬虫请求参数格式化

待格式化字符串1

get请求的原始数据

username=cjy51320342&password=3723&_token=lHEkhng3WCjuwS1xlisBhmWF3sSDZKdtgV442WZu&_t=1555916658731

打开editplus,ctrl+f

格式化结果如下,最后一行自行调整

'username':'cjy51320342',
'password':'3723',
'_token':'lHEkhng3WCjuwS1xlisBhmWF3sSDZKdtgV442WZu',
_t=1555916658731

待格式化字符串2

从Fiddler的WebForms拷贝下来的原始数据

email    18895358020
icode    
origURL    http://www.renren.com/home
domain    renren.com
key_id    1
captcha_type    web_login
password    3b48dec6c35d02e4e3619a845a3150460e03344529ac6c26a532b70448fb9d0f
rkey    31f2cea97b6657f3ef072f0195443bcd
f    http%3A%2F%2Fwww.renren.com%2F970143672

正则如下

格式化后结果

'email':'18895358020',

'icode':'',

'origURL':'http://www.renren.com/home',

'domain':'renren.com',

'key_id':'1',

'captcha_type':'web_login',

'password':'3b48dec6c35d02e4e3619a845a3150460e03344529ac6c26a532b70448fb9d0f',

'rkey':'31f2cea97b6657f3ef072f0195443bcd',

f    http%3A%2F%2Fwww.renren.com%2F970143672

 3.Java字节码无用的行号前缀"/* xxx */"

idea不存在这种问题,eclipse反编译的字节码会有大量的前缀

正则为/*s*d*s**/

源文件:

/*     */   public static <T> void writeExcel(String filename, List<HeaderDomain> header, List<T> list) throws Exception
/*     */   {
/* 146 */     if (NonUtil.isNon(list))
/*     */     {
/* 148 */       BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filename));
/* 149 */       XSSFWorkbook workbook = WorkbookFactory.createXSSF();
/* 150 */       workbook.write(bos);
/* 151 */       bos.flush();
/* 152 */       bos.close();
/* 153 */       return;
/*     */     }
/*     */     
/*     */ 
/* 157 */     Class<?> cls = list.get(0).getClass();
/* 158 */     MethodAccess methodaccess = MethodAccess.get(cls);
/*     */     
/* 160 */     File file = new File(filename);
/* 161 */     if (!file.exists()) {
/* 162 */       file.getParentFile().mkdirs();
/*     */     }
/*     */     
/* 165 */     BufferedOutputStream bos = null;
/*     */     try {
/* 167 */       bos = new BufferedOutputStream(new FileOutputStream(filename));
/* 168 */       XSSFWorkbook workbook = WorkbookFactory.createXSSF();
/* 169 */       XSSFSheet sheet = workbook.createSheet();
/*     */       
/*     */ 
/* 172 */       XSSFRow row = sheet.createRow(0);
/* 173 */       for (int headerindex = 0; headerindex < header.size(); headerindex++) {
/* 174 */         XSSFCell cell = row.createCell(headerindex);
/* 175 */         cell.setCellType(1);
/* 176 */         cell.setCellValue(((HeaderDomain)header.get(headerindex)).getName());
/*     */       }
/*     */       
/*     */ 
/* 180 */       int rowcount = list.size();
/* 181 */       for (int curdataindex = 0; curdataindex < rowcount; curdataindex++) {
/* 182 */         row = sheet.createRow(curdataindex + 1);
/*     */         
/* 184 */         T obj = list.get(curdataindex);
/*     */         
/* 186 */         int colindex = 0;
/* 187 */         for (HeaderDomain item : header) {
/* 188 */           String code = item.getCode();
/* 189 */           String getmethodname = "get" + code.replaceFirst(code.substring(0, 1), code.substring(0, 1).toUpperCase());
/*     */           
/* 191 */           Object value = methodaccess.invoke(obj, getmethodname, new Object[0]);
/* 192 */           if (NonUtil.isNon(value)) {
/* 193 */             value = "";
/*     */           }
/* 195 */           XSSFCell cell = row.createCell(colindex);
/* 196 */           cell.setCellType(1);
/* 197 */           cell.setCellValue(value.toString());
/*     */           
/* 199 */           colindex++;
/*     */         }
/*     */       }
/*     */       
/* 203 */       workbook.write(bos);
/* 204 */       bos.flush();
/*     */     } catch (Exception ex) {
/* 206 */       ex.printStackTrace();
/* 207 */       throw ex;
/*     */     } finally {
/* 209 */       if (bos != null) {
/*     */         try {
/* 211 */           bos.close();
/*     */         } catch (IOException e) {
/* 213 */           e.printStackTrace();
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ }

 替换之

这样就都去掉了

   public static <T> void writeExcel(String filename, List<HeaderDomain> header, List<T> list) throws Exception
   {
     if (NonUtil.isNon(list))
     {
       BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filename));
       XSSFWorkbook workbook = WorkbookFactory.createXSSF();
       workbook.write(bos);
       bos.flush();
       bos.close();
       return;
     }
     
 
     Class<?> cls = list.get(0).getClass();
     MethodAccess methodaccess = MethodAccess.get(cls);
     
     File file = new File(filename);
     if (!file.exists()) {
       file.getParentFile().mkdirs();
     }
     
     BufferedOutputStream bos = null;
     try {
       bos = new BufferedOutputStream(new FileOutputStream(filename));
       XSSFWorkbook workbook = WorkbookFactory.createXSSF();
       XSSFSheet sheet = workbook.createSheet();
       
 
       XSSFRow row = sheet.createRow(0);
       for (int headerindex = 0; headerindex < header.size(); headerindex++) {
         XSSFCell cell = row.createCell(headerindex);
         cell.setCellType(1);
         cell.setCellValue(((HeaderDomain)header.get(headerindex)).getName());
       }
       
 
       int rowcount = list.size();
       for (int curdataindex = 0; curdataindex < rowcount; curdataindex++) {
         row = sheet.createRow(curdataindex + 1);
         
         T obj = list.get(curdataindex);
         
         int colindex = 0;
         for (HeaderDomain item : header) {
           String code = item.getCode();
           String getmethodname = "get" + code.replaceFirst(code.substring(0, 1), code.substring(0, 1).toUpperCase());
           
           Object value = methodaccess.invoke(obj, getmethodname, new Object[0]);
           if (NonUtil.isNon(value)) {
             value = "";
           }
           XSSFCell cell = row.createCell(colindex);
           cell.setCellType(1);
           cell.setCellValue(value.toString());
           
           colindex++;
         }
       }
       
       workbook.write(bos);
       bos.flush();
     } catch (Exception ex) {
       ex.printStackTrace();
       throw ex;
     } finally {
       if (bos != null) {
         try {
           bos.close();
         } catch (IOException e) {
           e.printStackTrace();
         }
       }
     }
   }
 }
原文地址:https://www.cnblogs.com/Java-Starter/p/10750291.html