gpt4 book ai didi

c# - 解析 RegEx 模式

转载 作者:行者123 更新时间:2023-11-30 16:21:15 24 4
gpt4 key购买 nike

有什么方法可以解析一个复杂的正则表达式模式(包含几个命名组以及几个编号组非捕获组 ) 并报告每个 groupnamegroupnumber 以及模式文本。

假设,我有一个像这样的 RegEx 模式:

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

我喜欢提取:=

  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 模式的大型数据库。以前的程序员在准备这些复杂的模式时没有使用任何注释 [(?#...)],而且这些模式中不存在 linebreaks。在某些情况下,我必须修改这些模式,还必须在这些模式中使用注释。现在这就像大海捞针。为此,我根本无法使用 RegEx。因此,我倾向于在这种情况下使用解析器。

我尝试了什么:

我为此尝试了 GetGroupNamesGetGroupNumbers 集合。我只能提取 groupsNames/Numbers,但不能提取相应的文本模式。

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

最佳答案

这个怎么样,为此:

(?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 作为组号的键。如果您还想在输出中包含非捕获组和函数组,则可以更改该结构。

关于c# - 解析 RegEx 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13414238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com