2014-04-22 96 views
0

我想解析使用.NET正则表达式一些字符串,用格式,其中分隔符是'值和分离器&值:带分隔符正则表达式

'A$04'&'A&&&'&'585262&YY'&'05555' 

我发现的问题是,分隔符&也可以出现在每个单值中。

请问我可以怎么做,而不使用循环?我尝试了一些正则表达式,但没有成功。

+0

安置自己的正则表达式,你至今尝试过... –

+0

如果您不需要检查数据结构,你只能提取引号之间的内容。 –

+0

预期产量? – aelor

回答

0

尝试

'[^']+'(&'[^']+')* 

这应该工作,除非你可以有你的领域里撇号。请注意,我假设您的字段不能为空 - 将+替换为*以处理此情况。

0
string[] splitArray = null; 
try { 
    splitArray = Regex.Split(subjectString, "'(.*?)'"); 
} catch (ArgumentException ex) { 
    // Syntax error in the regular expression 
} 


Match the character “'” literally «'» 
Match the regular expression below and capture its match into backreference number 1 «(.*?)» 
    Match any single character that is not a line break character «.*?» 
     Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» 
Match the character “'” literally «'» 
0

另一种方式来分割:

string[] values = Regex.Split("'A$04'&'A&&&'&'585262&YY'&'05555'", "(?<=')&(?=')"); 

我们通过拆分通过&'前面和后面'

(?<=')&(?=') 

Regular expression visualization

Debuggex Demo

0

试试这个..

 var r = new Regex("'[A-Z0-9&$]*'",RegexOptions.IgnoreCase); 
     var matches = r.Matches("'A$04'&'A&&&'&'585262&YY'&'05555'"); 
     foreach (var match in matches) 
     { 
      var finalValue = match.ToString().Replace("'",""); 
     }