Raymond Osterbrink
Raymond Osterbrink

Reputation: 540

Regex: Keep unkknown substring

I try to modify a set of strings. I need to replace some unknown parts but also keep the unkknown middle part. In notepad++ I used this

as regex-input: .*(ThemeResource .*?Brush).*

and regex-output: /1

with this result:

Input:

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

Output:

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

but with c#-regex my output is allways: "/1"

I guess, there are fundamental differences between c# and notepad++ regex, but do not understand what to change, since my regex-input-selection seems to work as expected.

Edit:

My code:

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

My notepad++ "Find and Replace":

enter image description here

Upvotes: 2

Views: 60

Answers (1)

Thomas Ayoub
Thomas Ayoub

Reputation: 29451

To get the capturing group, use $1 instead of /1:

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

Upvotes: 2

Related Questions