gpt4 book ai didi

.net - 结合 .net 和 powershell 正则表达式捕获组语法

转载 作者:行者123 更新时间:2023-12-03 01:26:56 26 4
gpt4 key购买 nike

我在组合 powershell native 正则表达式捕获组语法时遇到问题 $n使用 .net 一个 $args.groups[n].value .
HTML 代码如下:

ab
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<b style="color:black;font-size:110%">ab</b><br>
<span class="WordHead"><b>ab</b></span> <span class="IPA">[ap]</span><br>
<span class="RomArticles"><span class="RomNum">Ⅰ.</span><a id="Ⅰ."></a><i><font color="darkblue">adv</font></i></span><br>
<span class="NumBracket">&nbsp;<b><font color="sienna">1)</font></b><a id="1)"></a> <i><font color="darkgreen">(weg, entfernt)</font></i> off;</span><br>
<span class="AllExamples">&nbsp;&nbsp;<b><font color="#5b4636">zur Post geht es an der Kreuzung links ~</font></b> the post office is off to the left at the crossroads;<br>
&nbsp;&nbsp;<b><font color="#5b4636">~ sein</font></b> to be out in the sticks;<br>
&nbsp;&nbsp;<b><font color="#5b4636">weit ~ sein</font></b> [<i><font color="black">o</font></i> <b>liegen</b>] to be far away;<br>
&nbsp;&nbsp;<b><font color="#5b4636">das Lokal ist mir zu weit ab</font></b> the pub is too far away;<br>
&nbsp;&nbsp;<b><font color="#5b4636">das liegt zu weit ~ vom Weg</font></b> that's too far off the beaten track</span><br>
<span class="NumBracket">&nbsp;<b><font color="sienna">2)</font></b><a id="2)"></a> <i><font color="darkgreen">(abgetrennt)</font></i> off;</span><br>
<span class="AllExamples">&nbsp;&nbsp;<b><font color="#5b4636">~ sein</font></b> <abbr title="informal" class="Icon">fam</abbr> to be broken [off];<br>
&nbsp;&nbsp;<b><font color="#5b4636">mein Knopf ist ab</font></b> I've lost a button;<br>
&nbsp;&nbsp;<b><font color="#5b4636">erst muss die alte Farbe ~</font></b> first you have to remove the old paint</span><br>
<span class="NumBracket">&nbsp;<b><font color="sienna">3)</font></b><a id="3)"></a> <i><font color="darkgreen">(in Befehlen)</font></i> off;</span><br>
<span class="AllExamples">&nbsp;&nbsp;<b><font color="#5b4636">~ ins Bett!</font></b> off to bed!;<br>
&nbsp;&nbsp;<b><font color="#5b4636">~, ihr beiden, Hände waschen!</font></b> off you two go, and wash your hands!;<br>
&nbsp;&nbsp;<b><font color="#5b4636">~ nach Hause!</font></b> off home with you!;<br>
&nbsp;&nbsp;<b><font color="#5b4636">~ in</font></b>/<b><font color="#5b4636">auf dein Zimmer!</font></b> go to your room!;<br>
&nbsp;&nbsp;<b><font color="#5b4636">~ nach oben</font></b>/<b><font color="#5b4636">unten!</font></b> up/down we/you etc. go!;<br>
&nbsp;&nbsp;<b><font color="#5b4636">~ sofort</font></b> as of now;<br>
&nbsp;&nbsp;<b><font color="#5b4636">~ und zu</font></b> [<i><font color="black">o</font></i> <span class="region"><abbr title="Northern German" class="nordd">NORDD</abbr></span> <b>an</b>] now and then</span><br>
<span class="NumBracket">&nbsp;<b><font color="sienna">4)</font></b><a id="4)"></a> <i><font color="darkgreen">(abgehend)</font></i> from;</span><br>
<span class="AllExamples">&nbsp;&nbsp;<b><font color="#5b4636">der Zug fährt ~ Köln</font></b> the train departs from Cologne;<br>
&nbsp;&nbsp;<b><font color="#5b4636">Frankfurt ~ 19 Uhr, New York an 8 Uhr</font></b> departing Frankfurt [at] 19.00, arriving New York [at] 8.00 </span><br>
<span class="RomArticles"><span class="RomNum">Ⅱ.</span><a id="Ⅱ."></a><i><font color="darkblue">präp</font> <font color="black">+dat</font></i></span><br>
<span class="NumBracket">&nbsp;<b><font color="sienna">1)</font></b><a id="1)"></a> <i><font color="darkgreen">(räumlich)</font></i> from</span><br>
<span class="NumBracket">&nbsp;<b><font color="sienna">2)</font></b><a id="2)"></a> <i><font color="darkgreen">(zeitlich)</font></i> from;</span><br>
<span class="AllExamples">&nbsp;&nbsp;<b><font color="#5b4636">~ wann ...?</font></b> from when ...?</span><br>
<span class="NumBracket">&nbsp;<b><font color="sienna">3)</font></b><a id="3)"></a> <i><font color="darkgreen">(von ... aufwärts)</font></i> from;</span><br>
<span class="AllExamples">&nbsp;&nbsp;<b><font color="#5b4636">Kinder ~ 14 Jahren</font></b> children from the age of 14 up</span><br>
<span class="NumBracket">&nbsp;<b><font color="sienna">4)</font></b><a id="4)"></a> <span class="Categories">ökon</span> ex;</span><br>
<span class="AllExamples">&nbsp;&nbsp;<b><font color="#5b4636">Preis ~ Fabrik</font></b>/<b><font color="#5b4636">Werk</font></b> price ex factory/works</span><br>
<span class="NumBracket">&nbsp;<b><font color="sienna">5)</font></b><a id="5)"></a> <span class="region"><abbr title="Swiss" class="schweiz">SCHWEIZ</abbr></span> <i><font color="darkgreen">(nach der Uhrzeit)</font></i> past;</span><br>
<span class="AllExamples">&nbsp;&nbsp;<b><font color="#5b4636">Viertel ~ 8</font></b> quarter past eight</span><br>
<span class="NumBracket">&nbsp;<b><font color="sienna">6)</font></b><a id="6)"></a> <span class="region"><abbr title="Swiss" class="schweiz">SCHWEIZ</abbr></span> <i><font color="darkgreen">(von)</font></i> on;</span><br>
<span class="AllExamples">&nbsp;&nbsp;<b><font color="#5b4636">~ Kassette</font></b> on cassette</span><br>

我想在 <a id="n)"></a> 中插入罗马数字标签。
这是我的代码不起作用:
$content = [System.IO.File]::ReadAllText("C:\test.txt", [System.Text.Encoding]::UTF8)
$result = [regex]::Replace( $content, '(?smi)<a id="([Ⅰ-Ⅹ]\.)"></a>(?:(?!<br>).)+<br>\r\n\t(?:(?!<br>).)+<a id="\d*\)"></a>(?:(?!</>|\t<span class="(?:RomArticles|NumDotArticles|Phrases)">).)+(?=</>|\t<span class="(?:RomArticles|Phrases|NumDotArticles)">)',
{$args.value -replace '(<a id=")(\d*\)"></a>)', "$1$args.groups[1].value$2"})

如何将这两种不同的语法混合到一段代码中以获得我想要的结果?

最佳答案

只有一种语法 - -replace内部调用 Regex.Replace() , 和 Regex.Replace()还支持$N引用。

您的问题是双重的 - 首先,当您使用双引号时,如下所示:"$1" , PowerShell 将尝试扩展/解析 $1作为替换模式之前的变量传递给 -replace .

第二个问题是 PowerShell 只扩展双引号字符串中的变量值,而不是整个表达式。为此,您需要将表达式括在子表达式 $() 中。 .

所以,要么逃避 $使用反引号 ( ` ) 并将表达式括在 $() 中,或使用单引号字符串:

$args.value -replace '(<a id=")(\d*\)"></a>)', "`$1$($args.groups[1].value)`$2"

...或在调用 -replace 之前单独准备字符串,也许是 -f字符串格式运算符:
$sub = '$1{0}$2' -f $args.Groups[1].Value
$args.Value -replace '(<a id=")(\d*\)"></a>)', "`$1$($args.groups[1].value)`$2"

如果您切换到更新版本的 PowerShell(6.1 或更新版本),则无需调用 Regex.Replace直接利用动态匹配评估器:
$content -replace $pattern,{
return $_.Value -replace '(<a id=")(\d*\)"></a>)', "`$1$($match.Groups[1].Value)`$2"
}

关于.net - 结合 .net 和 powershell 正则表达式捕获组语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61819689/

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