在項目中,遇到需求,需要進行規則入庫,想到使用正則進行表達式的拆分和分類,具體如下: 目標是:拆分介於表示邏輯運算的“And”或者“Or”的子句,比如:Operation Mode(Operation Mode_2) (Approve CR1) equals Accept;Need Physical ...
在項目中,遇到需求,需要進行規則入庫,想到使用正則進行表達式的拆分和分類,具體如下:
Operation Mode(Operation Mode_2) (Approve CR1) equals Accept And ( Need Physical Access(Need Physical Access) (Create CR) does not equal Switch Site Or ( Need Physical Access(Need Physical Access) (Create CR) equals Switch Site And Region(Region) (Create CR) equals R10 ) ) And ( Change Type(ChangeType_CreateCR) (Create CR) belongs to PM / CM CR,BTS_Modernization,FDD/TDD,Cell_Upgrade Or Change Type(ChangeType_CreateCR) (Create CR) belongs to PAT and FAT and FAC_NSA,Health Check for TX,Survey_Visit NSA,Site Cleanup,Site Battery Test,BSC/RNC Software Upgrade,OTDR/Fusion,APN Creation/Modification,Firewall,Facility_Non Telecom_NSA Or Change Type(ChangeType_CreateCR) (Create CR) belongs to WiMAX MW Upgrade,RAN MW Upgrade,FDD and WIMAX,TDD and WIMAX,FDD/TDD and WIMAX,Change time_Not implemented CR Or Change Type(ChangeType_CreateCR) (Create CR) belongs to SiteLevel_QOS test_QOS implementation,RAN CRs,TXCore_NSA without physical change,TXCore_NSA including physical change,CoreCS_NSA without physical change,CorePS_NSA without physical change,CorePS_NSA including physical change,IP NSA_without physical change,IP NSA_including physical change,CoreCS_NSA including physical change Or Change Type(ChangeType_CreateCR) (Create CR) belongs to LAC / TAK Modification,Site Sharing,Rehoming Site,BSC/RNC_Unit check/replace,RAN LOS Checking,IP_Installation / Integration / Dismantling,Net Security Access,PAT/FAT/FAC_SA_SiteLevel,SiteLevel_installation_Change_DismantlingEquipment,SiteLevel_SoftwareUpgrade,SiteLevel_Anti Theft Belt Installation,Connecting External Fiber To Internal Fiber In Site,SiteLevel_PDB Cabling And Fuse Change,BSC/RNC_OSS Upgrade,TX Core_V LAN Adding,WiMAX_Core Level,BSC/RNC_NSA_installation / integration / dismantling Or Change Type(ChangeType_CreateCR) (Create CR) belongs to Site Level_Rerouting/Relocation,BSC/RNC_Redundancy Board Check,WiMAX_Site or TX Level including physical change,WiMAX_Site or TX Level without physical change,PM/CM_WiMAX SA Or Change Type(ChangeType_CreateCR) (Create CR) belongs to TX Core_OSS Upgrade,IP_OSS Upgrade,SiteLevel_Clean up_NSA,SiteLevel_Clean up_SA,TX Access_Without physical change,TX Access_Including physical change,SiteLevel_Software changes,BSC/RNC Software changes,SDM Or Change Type(ChangeType_CreateCR) (Create CR) belongs to BSS-BTS Level,Core-CS,Core-PS,TX-Core,IP-SA,WiMAX-BSS and TX,WiMAX-Core,Survey/Visit Including Tower Activity Or Change Type(ChangeType_CreateCR) (Create CR) belongs to Go_Live,Gi APN Creation/Modification,MW Performance,Sunshade Installation,BTS Halt Or Change Type(ChangeType_CreateCR) (Create CR) belongs to IP_RAN,TX_SA,Fiber,Gi Lan,PM_SiteLevel_SA Or Change Type(ChangeType_CreateCR) (Create CR) belongs to Optimization_High SA,Roaming ) |
目標是:拆分介於表示邏輯運算的“And”或者“Or”的子句,比如:Operation Mode(Operation Mode_2) (Approve CR1) equals Accept;Need Physical Access(Need Physical Access) (Create CR) does not equal Switch Site等子句,以便於後續的處理;
正則表達式的思路:上述字元串的子句是“Operation Mode”等諸如此類的關鍵字+括弧+括弧+equals等表示所屬關係的關鍵字+各類單項,
那麼,
1.首先整理出來,用於匹配開頭的關鍵字的子句,記作K1:(Operation Mode|Need Physical Access|Change Type|Region)
2.接下來是整理出匹配equals等表示所屬關係的關鍵字,記作K2:( belongs to | equals | contains | does not equal | does not contain | does not belong to )
3.接下來是整理出匹配兩個連續括弧部分的第一個括弧,記作S1,這裡有這麼幾類:(字元串+空格+字元串)
那麼對應的S1為:((\((\w* ?\w*)*\))|(\((\w*_\w*)*\))) :其中(\w* ?\w*)用於匹配(字元串+空格+字元串)(單個字元串)這兩種格式,(\w* ?\w*)*用於匹配上述兩種格式的多個字元串,如:(Need Physical Access) 。其中(\w*_\w*)*用於匹配(字元串_字元串)的這種情況;
4.接下來是整理出匹配兩個連續括弧部分的第二個括弧,記作S2,這裡僅有一類:(字元串+空格+字元串)
那麼對應的S2為:這地方第二個括弧的情況包含於第一類,所以令S2=S1
5.接下來,接下來需要取出表示所屬關係的字元串後邊的欄位,這裡有如下情況,在所屬關係的字元串後邊的欄位中,有 “字元串空格And(”或者“字元串+Or( ”或者“And”或者“Or”,那麼,可以使用不包含欄位:((?!所要剔除的字元串).)*,但是,這個地方需要註意的是,有些關係運算符後邊的字元串也包含And或者Or,那麼,我們需要進行如下處理,需要提出的只是And或者Or後邊是K1匹配出來的子串,S3表達式如下:
S3:((?!( And \(|Or \(| \)| And (K1S1 S1)| Or (K1S1 S1))).)*
那麼,最終的表達式如下所示K1S1 S2 K2 S3,
即:(Operation Mode|Need Physical Access|Change Type|Region)((\((\w* ?\w*)*\))|(\((\w*_\w*)*\))) ((\((\w* ?\w*)*\))|(\((\w*_\w*)*\)))( belongs to | equals | contains | does not equal | does not contain | does not belong to )((?!( And \(|Or \(| \)| And ((Operation Mode|Need Physical Access|Change Type|Region)((\((\w* ?\w*)*\))|(\((\w*_\w*)*\))) ((\((\w* ?\w*)*\))|(\((\w*_\w*)*\))))| Or ((Operation Mode|Need Physical Access|Change Type|Region)((\((\w* ?\w*)*\))|(\((\w*_\w*)*\))) ((\((\w* ?\w*)*\))|(\((\w*_\w*)*\)))))).)*
匹配的效果如下所示:
供大家參考一下~~