gpt4 book ai didi

c# - 捕获组,每行很多组

转载 作者:太空宇宙 更新时间:2023-11-03 20:15:33 24 4
gpt4 key购买 nike

我有一些带有“控制序列”的 html,我已经从内容中删除了 html 标签,现在我想将“控制序列”变成“样式”。

删除 html 标签后我有这个...

"<!C43!><!TG!>Some Characters"

最终产生这个...

<span class="C43 TG">Some Characters</span>

到目前为止我的 C# 有问题:

Regex reg = new Regex("<!([^<>]+?)!>");

Match matches = reg.Match(line);
foreach (Group group in matches.Groups)
{
// finds both groups,
// and remove the control sequence
}

我的“foreach”中还没有太多内容,因为当我用断点检查它时,它错误地返回了以下组...

Group 1 : <!C43!>
Group 2 : C43
<it does not find second group :( >

任何帮助将不胜感激,但我主要是在寻找正确的正则表达式来找到我在字符串中寻找的内容,但我对正则表达式库也毫无用处,所以最有效的“查找组” , store group, remove group from string i'm searching' 也将不胜感激。

最佳答案

你循环错了。试试这个:

string line = ...;
Regex reg = ...;
for (var match = reg.Match(line); match.Success; match = match.NextMatch())
{
// in here, don't bother with .Groups... you don't need it
}

关于c# - 捕获组,每行很多组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16967124/

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