2015-09-22 62 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