我如何在我的程序中使用matchevaluator函数? [英] How do I matchevaluator function in my program?
问题描述
我正在尝试搜索正则表达式
I'm trying to search a regular expression
pattern
,如果匹配,则查找该模式的值是否存在于<形式的任何标记内; sec id =sec123>在一个文件中。如果是,我想用
and, if it matches, find whether the value of that pattern exists inside any tag of the form "<sec id="sec123">" in a file. If it does, I want to replace it with
result1
替换它。我认为可以使用MatchEvaluator函数完成,但我无法弄清楚如何应用它。
我是VB.NET的新手(和编程)一般而言)并且真的不知道该怎么做。这是我到目前为止所尝试的:
样本输入:
. I think it can be done with the MatchEvaluator function, but I can't figure out how to apply it.
I'm new to VB.NET (and programming in general) and really don't know what to do. This is what I've tried so far:
sample input:
<sec id="sec1">
<p>"You fig. 23 did?" I <xref ref-type="section" rid="sec12">section 12</a> asked, surprised.</p>
<p>"There are always better terms <xref ref-type="section" rid="sec6">section 6</a>, Richard!" my mom said sharply.</p>
<p>I <xref ref-type="section" rid="sec2">section 2</a> stood. I <xref ref-type="section" rid="sec2">section 2</a> had to hurry if I <xref ref-type="section" rid="sec1">section 1</a> was going to get to work on time.
<fig id="fig4">
<caption><p>I'm confused</p></caption>
</fig>
</p>
<p>Turning to face her, I <xref ref-type="section" rid="sec2">section 2</a> walked backward. "I"ve seriously got to get ready. Why don"t we get together for lunch and talk more then?"</p>
<sec id="sec2">
<p>"You fig. 23 can"t be""</p>
<p>I <xref ref-type="section" rid="sec4">section 4</a> adored the Art Deco elegance of the Chrysler Building. I <xref ref-type="section" rid="sec2">section 2</a> could pinpoint my place on the island in relation to the posit table 9ion of the Empire State Building.</p>
<p>I <xref ref-type="section" rid="sec1">section 1</a> felt Gideon before I <xref ref-type="section" rid="sec1">section 1</a> saw him, my entire body humming wit table 9h awareness as he stepped out of the Bentley, which had pulled up behind the Benz.</p>
</sec>
</sec>
我希望程序找到文件中的所有 rid =secX元素并检查是否 secX元素存在于任何表达式中< sec id =secX>在整个文件中,如果存在不匹配,则< xref ref-type =sectionrid =secX> section X< / a>将被删除到 X部分,这将继续,直到没有< b> rid =secX< / b>
表达式留下来检查
我尝试过:
I want the program to find all rid="secX" elements in the file and check whether that "secX" element is present inside any of the expressions <sec id="secX"> in the entire file and if there is a mismatch, then the <xref ref-type="section" rid="secX">section X</a> will be removed to section X and this will go on until there is no <b>rid="secX"</b>
expression is left to check
What I have tried:
Dim pattern As String="(?<=rid=\"sec)(\\d+)(?=\">)"
Dim r As Regex = New Regex(pattern)
Dim m As Match = r.Match(input)
If (m.Success) Then
Dim x As String=" id=""sec"+ pattern +""""
Dim r2 As Regex = New Regex(x)
Dim m2 As Match = r2.Match(input)
If (m2.Success) Then
Dim tgPat AsString="<xref ref-type="section" rid=""sec + pattern +"">(\w+) (\d+)</a>"
Dim tgRep As String= "$1 $2"
Dim tgReg As New Regex(tgPat)
Dim result1 As String = tgReg.Replace(input, tgRep)
Else
EndIf
EndIf
Next
推荐答案
1
2
Dim tgReg 作为 新正则表达式(tgPat)
Dim result1 As String = tgReg.Replace(input,tgRep)
否则
EndIf
EndIf
下一步
2" Dim tgReg As New Regex(tgPat) Dim result1 As String = tgReg.Replace(input, tgRep) Else EndIf EndIf Next
这样的事情应该有效:
Something like this should work:
Dim xref As New Regex("<xref[^>]+rid=""(?<id>sec\d+)""[^>]*>(?<content>[^<]+)</xref>")
Dim result As String = xref.Replace(input, Function(match)
Dim sec As New Regex(" id=""" & match.Groups("id").Value & """")
Return If(sec.IsMatch(input), match.Value, match.Groups("content").Value)
End Function)
但是,你应该仔细检查你的输入。它几乎看起来像HTML,除了你有一个开头的< xref>
标签以< / a>
结束标签,它不匹配。
如果输入是 HTML,你可能会更好运使用像AngleSharp这样的HTML解析器来解析并修改文件。
正则表达式语言 - 快速参考 [ ^ ]
AngleSharp - Home [ ^ ]
However, you should double-check your input. It almost looks like HTML, except you have an opening <xref>
tag closed with an </a>
tag, which doesn't match.
If the input is HTML, you might have better luck using an HTML parser like AngleSharp to parse and modify the document.
Regular Expression Language - Quick Reference[^]
AngleSharp - Home[^]
这篇关于我如何在我的程序中使用matchevaluator函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!