2015-09-22 61 views
-2

有什麼辦法來分割下文提到的字符串轉換成使用正則表達式組[用括號分組]如何將字符串分割成組,匹配paranthisisbin在C#

Input: "[Id][email protected],{buyerid}<>@id,[userid]>@userid,{custid}[email protected],status>1" 

條件:

1.Need到在單獨的csvs和不帶圓括號的表達式中分隔類似的括號表達式。括號將只在左側operator.Allowed運營商是=,>,<>,<,!=

[ anytext    ] = @anytetx 
{ anytext    } [email protected] 
anytext withot parathis [email protected] 

2.運營商的左側,始終存在一些純文本沒有任何括號專包機或者沒有括號的情況下,但運營商的右側有可以實現文字,如:@accountid,「ACCOUNTID」,並於1983年1月1日##和布爾值true或false 輸出數字和日期:

1. "[Id][email protected],[userid]>@userid" 

2. "{buyerid}<>@id,{custid}[email protected]" 

3. "status>1" 
+0

有辦法做到這一點沒有正則表達式。你在用什麼語言? –

+1

請詳細說明條件。向我們展示其他一些例子。邊緣案例呢?任何例外? ...更重要的是,你到目前爲止嘗試過什麼? – Mariano

+0

要考慮的具體邊緣情況:包含逗號的字段,包含引號的字段。 – Richard

回答

0

當然,這裏是你如何在Python中做到這一點。

1.py:

import re 

txt='[Id][email protected],{buyerid}<>@id,[userid]>@userid,{custid}=[email protected],status>1' 

re1='(.*?),' # Command Seperated Values 1 
re2='.*?' # Non-greedy match on filler 
re3='.*?,' # Uninteresting: csv 
re4='.*?' # Non-greedy match on filler 
re5='.*?,' # Uninteresting: csv 
re6='.*?' # Non-greedy match on filler 
re7='(.*?),' # Command Seperated Values 2 

rg = re.compile(re1+re2+re3+re4+re5+re6+re7,re.IGNORECASE|re.DOTALL) 
m = rg.search(txt) 
if m: 
    csv1=m.group(1) 
    csv2=m.group(2) 
    print "("+csv1+")"+"("+csv2+")"+"\n" 

2.py:

import re 

txt='[Id][email protected],{buyerid}<>@id,[userid]>@userid,{custid}[email protected],status>1' 

re1='.*?' # Non-greedy match on filler 
re2='.*?,' # Uninteresting: csv 
re3='(.*?),' # Command Seperated Values 1 
re4='.*?' # Non-greedy match on filler 
re5='(\\{custid\\}[email protected])' # Command Seperated Values 2 

rg = re.compile(re1+re2+re3+re4+re5,re.IGNORECASE|re.DOTALL) 
m = rg.search(txt) 
if m: 
    csv1=m.group(1) 
    csv2=m.group(2) 
    print "("+csv1+")"+"("+csv2+")"+"\n" 

3.py:

import re 

txt='[Id][email protected],{buyerid}<>@id,[userid]>@userid,{custid}[email protected],status>1' 

re1='.*?' # Non-greedy match on filler 
re2='(status)' # Word 1 
re3='(>)' # Any Single Character 1 
re4='(\\d+)' # Integer Number 1 

rg = re.compile(re1+re2+re3+re4,re.IGNORECASE|re.DOTALL) 
m = rg.search(txt) 
if m: 
    word1=m.group(1) 
    c1=m.group(2) 
    int1=m.group(3) 
    print "("+word1+")"+"("+c1+")"+"("+int1+")"+"\n" 
+0

你好OlivierBlanvillain,我已經更新了問題 – Rakesh