gpt4 book ai didi

c# - 正则表达式:保留未知子串

转载 作者:行者123 更新时间:2023-11-30 23:30:09 25 4
gpt4 key购买 nike

我尝试修改一组字符串。我需要更换一些未知的部分,但也要保留未知的中间部分。在 Notepad++ 中我使用了这个

作为正则表达式输入:.*(ThemeResource .*?Brush).*

和正则表达式输出:/1

结果是:

输入:

"<Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />"
"<Setter Property="Background" Value="{ThemeResource SystemControlBackgroundAltMediumLowBrush}" />"
"<Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundBaseMediumLowBrush}" />"
"<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlPageBackgroundAltMediumBrush}" />"

输出:

"ThemeResource SystemControlForegroundBaseHighBrush"
"ThemeResource SystemControlBackgroundAltMediumLowBrush"
"ThemeResource SystemControlForegroundBaseMediumLowBrush"
"ThemeResource SystemControlPageBackgroundAltMediumBrush"

但是使用 c#-regex 我的输出总是:"/1"

我想,c# 和 notepad++ regex 之间存在根本差异,但不明白要更改什么,因为我的 regex-input-selection 似乎按预期工作。

编辑:

我的代码:

List<string> ls = File.ReadLines(@"c:\cbTemplate.xml").ToList().Select(x=>Regex.Replace(x, ".*{(ThemeResource .*?Brush).*", @"\1"));

我的 Notepad++ “查找和替换”:

enter image description here

最佳答案

要获取捕获组,请使用 $1 而不是 /1:

List<string> ls = File.ReadLines(@"c:\cbTemplate.xml").ToList().Select(x=>Regex.Replace(x, ".*{(ThemeResource .*?Brush).*", @"$1"));

关于c# - 正则表达式:保留未知子串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35060646/

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