Regex.Split扫描不正确

时间:2015-08-21 01:07:15

标签: .net regex

我对此表达式有疑问:;(?!.*(\}|""|')|(\{|""|'))问题是:

这是原始字符串:

abc; def; lalala;
123;
456;
789;
'some string with ; on center';
'an string;
new line;
chars;';
{
this;
doens't;
be;
detected;
};

这就是我想要的:

index      item
---------- --------------------
0          abc
1          def
2          lalala
3          123
4          456
5          789
6          'Some string with ; on center'
7          'an string;
           new line; 
           chars;'
8          {
           this;
           doens't;
           be;
           detected;
           }

但是,这是返回的内容......

index      item
---------- --------------------
0          abc
1          def
2          lalala
3          123
4          456
5          789
6          'Some string with ; on center'
7          'an string;
8          new line
9          chars;'
           {
10         this;
11         doens't;
12         be;
13         detected;
           }

这个正则表达式我无法获得如上所述的文件行,并且我已经在几个在线正则表达式调试器上测试了它,没有得到任何方法。我使用SingleLine选项,但它们会使事情恶化。任何的想法?错误在哪里?我可以在哪里升级我想要的方式?

这是我的代码的一部分(在VB.NET中):

Public Shared Sub runApplication(ByVal appString As String)
  Dim lines As String() = regex.Split(appString, ";(?!.*(\}|""|')|(\{|""|'))")
  Const iStart$ = "^[\t\s]*"
  Const iSpaceTab = "[\t\s]*"
  Const iProperty As System.Text.RegularExpressions.RegexOptions = Text.RegularExpressions.RegexOptions.None
  'set Text.RegularExpressions.RegexOptions.IgnoreCase to ignore case language
  Dim varList As New Dictionary(Of String, String)
  Dim constList As New Dictionary(Of String, String)

  For i As UInt64 = 0 To lines.Length - 1
     Dim X As String = lines(i)
     lastItem = X
 '........

我使用的是Visual Basic .NET, .NET Framework 4.5

1 个答案:

答案 0 :(得分:1)

为什么不创建一个与您专门寻找的字符串相匹配的模式,而不是使用RegEx.Split?例如,像这样:

(?<=^|\n|;\s*)({[^}]*}|'[^']*'|.*?)(?=;)

查看working example

然后你需要做的就是让匹配通过它们循环:

For Each m As Match In RegEx.Matches(appString, "(?<=^|\n|;\s*)({[^}]*}|'[^']*'|.*?)(?=;)")
    Dim line As String = m.Value
    ' ...
Next