Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 每个匹配的正则表达式替换匹配_C#_Regex - Fatal编程技术网

C# 每个匹配的正则表达式替换匹配

C# 每个匹配的正则表达式替换匹配,c#,regex,C#,Regex,我对应该可以工作但由于某种原因不能工作的代码有点问题。 我的想法是我有两个字符串,我需要用另一个字符串中的文本替换其中的一些文本来修复一些错误。 首先,我的代码: var rx2 = new Regex(@"<[^><]*>|<[^<]*(<[^>]*>)*[^>]*>"); MatchCollection matchesorig = rx2.Matches(or); int c = 0; if (matchesorig.Coun

我对应该可以工作但由于某种原因不能工作的代码有点问题。 我的想法是我有两个字符串,我需要用另一个字符串中的文本替换其中的一些文本来修复一些错误。 首先,我的代码:

var rx2 = new Regex(@"<[^><]*>|<[^<]*(<[^>]*>)*[^>]*>");
MatchCollection matchesorig = rx2.Matches(or);
int c = 0;
if (matchesorig.Count > 0)
de = rx2.Replace(de, match =>
{
    var res = matchesorig[c].Value;
    c++;
    return res;
});
例外情况:

The argumente specified was outsider the interval of valid values.
parameter Name: i
在第行引发异常:

var res = matchesorig[c].Value;
C=24时

EDIT2:正如评论中指出的,问题de string有一些不一致之处,这些不一致之处破坏了正则表达式:

< fill-sd val = "< <a:solidFill> a:srgbClr"/"616A71 = val > </a:solidFill>">
”>
请注意
,在第二个部分中,我使用的正则表达式没有考虑到一个空格。这使得德队有26场比赛,而不是24场。处理空间很琐碎,但是在错误的地方处理空间又如何呢?我无法更改该字符串,因为我从internet服务获取该字符串时无法访问该字符串(他们有一个问题,我现在才发现),因此我需要按原样处理该字符串。想法

EDIT3:我想出了这个解决方案(现在测试)

||

有人发现问题/版本较短吗?

经过一些测试,它归结为以下正则表达式:

<[^<>]*>|<[^<>]*(<[^<>]*>\s{0,1})*[^<>]*>
|

感谢@Vache指出我测试了错误的字符串-duuhh

您的字符串是什么?您现在得到了什么结果?我得到的结果是var res=matchesorig[c]行中的一个执行选项;尝试在c=24时进行替换,此时应该只有0到23之间的匹配项。我马上发布字符串-必须重新生成字符串,这需要几分钟时间-很抱歉,如果您能分享一些示例,那就太好了。@Braj-添加到问题中我在
de
中看到26个匹配项。。。
<[^<>]*>|<[^<>]*(<[^<>]*>)*[^<>]*>|<[^<>]*<[^<>]*<[^<>]*>\s{0,1}[^<>]*>\s{0,1}(<[^<>]*>\s{0,1})*[^<>]*>
<[^<>]*>|<[^<>]*(<[^<>]*>\s{0,1})*[^<>]*>