解析RegEx模式

时间:2012-11-16 10:01:54

标签: c# regex vb.net

有没有办法解析Complex RegEx模式(包含多个named groups以及多个numbered groupsnon-capturing groups)并报告每个groupname或{{1与模式文本一起。

假设我确实有这样的RegEx模式:

groupnumber

我想提取:=

  (?im)(?<x>\b[a-s03]+\b)(?-i)(?<a>\p{L}+?,(?<b>.+?:(?<c>.+?;(?<d>.+?(?:\d|sample-text|(\k'x'|sos30))))))

此要求的目的:

我有一个庞大的复杂RegEx模式数据库。之前编写的程序员在准备这些复杂模式时没有使用任何注释[ Named groups: x==>(?<x>\b[a-s03]+\b) a==>(?<a>\p{L}+?,(?<b>.+?:(?<c>.+?;(?<d>.+?(?:\d|sample-text|(\k'x'|sos30)))))) b==>(?<b>.+?:(?<c>.+?;(?<d>.+?(?:\d|sample-text|(\k'x'|sos30))))) c==>(?<c>.+?;(?<d>.+?(?:\d|sample-text|(\k'x'|sos30)))) d==>(?<d>.+?(?:\d|sample-text|(\k'x'|sos30))) Numbered groups: 1==>(\k'x'|sos30) Non-capturing-groups: 1st==>(?:\d|sample-text|(\k'x'|sos30)) ],而且这些模式中不存在(?#...)。我必须在某些情况下修改这些模式,并且还必须在这些模式中使用注释。现在它就像在草垛中寻找一根针。我根本无法将RegEx用于此目的。所以,我倾向于在这种情况下使用解析器。

我尝试了什么:

为此,我尝试了linebreaksGetGroupNames集合。我只能提取GetGroupNumbers的{​​{1}},而不能提取相应的文字模式。

我正在寻找非RegEx解决方案/一些提示。

2 个答案:

答案 0 :(得分:3)

对此如何:

(?im)(?<x>\b[a-s03]+\b)(?-i)(?<a>\p{L}+?,(?<b>.+?:(?'c'.+?;(.+?(?:\d|sample-text|(\k'x'|sos30))))))

这是输出:

(0)<0>:     (?im)(?<x>\b[a-s03]+\b)(?-i)(?<a>\p{L}+?,(?<b>.+?:(?'c'.+?;(.+?(?:\d|sample-text|(\k'x'|sos30))))))
(1)<x>:     \b[a-s03]+\b
(2)<a>:     \p{L}+?,(?<b>.+?:(?'c'.+?;(.+?(?:\d|sample-text|(\k'x'|sos30))))
(3)<b>:     .+?:(?'c'.+?;(.+?(?:\d|sample-text|(\k'x'|sos30)))
(4)<c>:     .+?;(.+?(?:\d|sample-text|(\k'x'|sos30))
(5)<5>:     .+?(?:\d|sample-text|(\k'x'|sos30)
(6)<6>:     \k'x'|sos30

这是代码:

Imports System.Collections.Specialized
Module Module1
Public DictGroups As New OrderedDictionary
Public DictTrackers As New Dictionary(Of Integer, Boolean)
Public intGroups As Integer = 0
Public CommandGroup As Boolean = False
Sub Main()
    Dim regexToEval As String = "(?im)(?<x>\b[a-s03]+\b)(?-i)(?<a>\p{L}+?,(?<b>.+?:(?'c'.+?;(.+?(?:\d|sample-text|(\k'x'|sos30))))))"
    Dim curChar As String = ""
    DictGroups.Add(0, "(0)<0>: " & vbTab)
    DictTrackers.Add(0, True)
    For i = 1 To regexToEval.Length
        Dim iChar As String = regexToEval.Substring(i - 1, 1)
        If curChar <> "\" AndAlso iChar = ")" Then EndGroup()
        AddStrToTrackers(iChar)
        If curChar = "\" OrElse iChar <> "(" OrElse regexToEval.Length < i + 2 Then curChar = iChar : Continue For
        If regexToEval.Substring(i, 1) = "?" Then
            i += 1 : AddStrToTrackers("?")
            If regexToEval.Substring(i, 1) = ":" Then i += 1 : AddStrToTrackers(":") : curChar = ":" : Continue For
            Dim NameLength As Integer = 0
            If regexToEval.Substring(i, 1) = "<" Or regexToEval.Substring(i, 1) = "'" Then
                i += 1 : AddStrToTrackers(regexToEval.Substring(i - 1, 1))
                i += 1
                For x = i To regexToEval.Length
                    If regexToEval.Substring(x - 1, 1) = ">" Or regexToEval.Substring(x - 1, 1) = "'" Then
                        NameLength = x - i
                        Exit For
                    End If
                Next
            Else
                CommandGroup = True
                Continue For
            End If
            If NameLength > 0 Then
                Dim GroupName As String = regexToEval.Substring(i - 1, NameLength)
                i += NameLength : curChar = regexToEval.Substring(i - 1, 1) : AddStrToTrackers(GroupName & curChar)
                intGroups += 1
                DictGroups.Add(intGroups, "(" & DictGroups.Count & ")<" & GroupName & ">: " & vbTab)
                DictTrackers.Add(intGroups, True)
                Continue For
            End If
        End If
        curChar = iChar
        intGroups += 1
        DictGroups.Add(intGroups, "(" & DictGroups.Count & ")<" & intGroups.ToString & ">: " & vbTab)
        DictTrackers.Add(intGroups, True)
    Next
    Dim Output As String = MakeOutput()
End Sub

Private Function MakeOutput() As String
    Dim retString As String = String.Empty
    For i = 0 To DictGroups.Count - 1
        retString &= DictGroups(i) & vbCrLf
    Next
    Return retString
End Function

Public Sub EndGroup()
    If CommandGroup Then
        CommandGroup = False
        Exit Sub
    End If
    Dim HighestNum As Integer = 0
    For Each item In DictTrackers
        If Not item.Value Then Continue For
        If item.Key > HighestNum Then HighestNum = item.Key
    Next
    If HighestNum <> 0 Then DictTrackers(HighestNum) = False
End Sub

Public Sub AddStrToTrackers(ByVal addString As String)
    For Each item In DictTrackers
        If item.Value Then DictGroups(item.Key) &= addString
    Next
End Sub
End Module

唯一的区别是我没有捕获非捕获组和功能组。当然,这只是我在10分钟内制作的快速代码。但如果你想要的话,这是一个开始。我使用OrderedDictionary作为Group-Numbers的键。如果您还想在输出中包含非捕获组和功能组,则可以更改该结构。

答案 1 :(得分:0)

System.Text.RegularExpressions命名空间中有一个RegexParser类(内部),您可以使用私有反射调用它。到目前为止,我在我的FxCopContrib项目中使用了sample implementation

您可以利用RegexParser implementation from the Mono project

然后是Deveel's Regex library