如何使用“MatchEvaluator"减少编码的功能? [英] How to use "MatchEvaluator" function to reduce coding?

查看:21
本文介绍了如何使用“MatchEvaluator"减少编码的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试搜索正则表达式 pattern,如果匹配,则查找该模式的值是否存在于任何形式为 <sec id="sec123"> 在一个文件中.如果是这样,我想用 result1 替换它.我认为可以使用 MatchEvaluator 函数来完成,但我不知道如何应用它.

I'm trying to search the regular expression pattern 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. I think it can be done with the MatchEvaluator function, but I can't figure out how to apply it.

我是 VB.NET(以及一般编程)的新手,真的不知道该怎么做.这是我迄今为止尝试过的:

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:

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+)</xref>"
        Dim tgRep As String= "$1 $2"
        Dim tgReg As New Regex(tgPat)
        Dim result1 As String = tgReg.Replace(input, tgRep)
    Else
    EndIf
EndIf
Next

样本输入:

<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>

预期输出:

<sec id="sec1">
<p>"You fig. 23 did?" I **section 12** asked, surprised.</p>
<p>"There are always better terms **section 6**, 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 **section 4** 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>

推荐答案

我已经用一个使用 XElement 的解决方案替换了我的初始解决方案,因为您的数据并不像我最初想象的那么简单.

I've replaced my initial solution with one which uses XElement as your data isn't as straightforward as I initially thought.

    Dim input = XElement.Parse(data)
    Dim sections = input.Descendants("sec").ToDictionary(Function(s) s.@id, Function(s) s)
    Dim xrefs = input.Descendants("xref").ToLookup(Function(s) s.@rid, Function(s) s)

    For Each group In xrefs
        Dim section As XElement
        If sections.TryGetValue(group.Key, section) Then
            For Each xref In group
                xref.ReplaceWith(section)
            Next
        End If
    Next
    Dim output = input.ToString()

我认为这符合您的要求,尽管我不相信您的数据,因为 Section2 似乎是递归的.尝试一下,看看你的想法.步骤:

I think this does what you're after although I don't trust your data as Section2 seems to be recursive. Have a try and see what you think anyway. Steps:

  1. 解析xml
  2. 提取部分并在字典中按 id 键
  3. 提取外部参照(多个具有相同的键)并按 id 将它们添加到查找中
  4. 对于每个外部参照,检查它是否有一个部分.如果是,则更换它.
  5. 提取结果.

这篇关于如何使用“MatchEvaluator"减少编码的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆