Skip to content

Commit 1d4a1ad

Browse files
author
Sébastien Geiser
committed
Better match/group/capture manage in C# replace
1 parent 0f974d2 commit 1d4a1ad

File tree

4 files changed

+48
-33
lines changed

4 files changed

+48
-33
lines changed

RegexDialog/RegExToolDialog.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
<Image Source="{StaticResource VS}" />
7979
</MenuItem.Icon>
8080
</MenuItem>
81-
<MenuItem Header="Export C# Code in a new tab" >
81+
<MenuItem Header="Export C# Code In A New Tab" >
8282
</MenuItem>
8383
<Separator />
8484
<MenuItem Header="_Exit" Click="Exit_MenuItem_Click" >

RegexDialog/RegExToolDialog.xaml.cs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,37 @@ public partial class RegExToolDialog : Window
5757
private static readonly Regex cSharpReplaceBeforePartRegex = new Regex(@"(?<=^|\s)\#before(?=\s)(?<before>.*)(?<=\s)\#endbefore(?=\s|$)", RegexOptions.Singleline | RegexOptions.Compiled);
5858
private static readonly Regex cSharpReplaceAfterPartRegex = new Regex(@"(?<=^|\s)\#after(?=\s)(?<after>.*)(?<=\s)\#endafter(?=\s|$)", RegexOptions.Singleline | RegexOptions.Compiled);
5959

60-
public object ReplaceScript
60+
private string InjectInReplaceScript(string replaceScript)
6161
{
62-
get
63-
{
64-
Match beforeMatch = cSharpReplaceBeforePartRegex.Match(ReplaceEditor.Text);
65-
Match afterMatch = cSharpReplaceAfterPartRegex.Match(ReplaceEditor.Text);
62+
Match beforeMatch = cSharpReplaceBeforePartRegex.Match(ReplaceEditor.Text);
63+
Match afterMatch = cSharpReplaceAfterPartRegex.Match(ReplaceEditor.Text);
6664

67-
return csEval.LoadCode(Res.CSharpReplaceContainer
68-
.Replace("//code", cSharpReplaceSpecialZoneCleaningRegex.Replace(ReplaceEditor.Text, string.Empty))
69-
.Replace("//usings", cSharpReplaceUsingsPartRegex.Match(ReplaceEditor.Text).Groups["usings"].Value)
70-
.Replace("//global", cSharpReplaceGlobalPartRegex.Match(ReplaceEditor.Text).Groups["global"].Value)
71-
.Replace("//before", beforeMatch.Success ? beforeMatch.Groups["before"].Value : "return text;")
72-
.Replace("//after", afterMatch.Success ? afterMatch.Groups["after"].Value : "return text;"));
73-
}
65+
return replaceScript
66+
.Replace("//code", cSharpReplaceSpecialZoneCleaningRegex.Replace(ReplaceEditor.Text, string.Empty))
67+
.Replace("//usings", cSharpReplaceUsingsPartRegex.Match(ReplaceEditor.Text).Groups["usings"].Value)
68+
.Replace("//global", cSharpReplaceGlobalPartRegex.Match(ReplaceEditor.Text).Groups["global"].Value)
69+
.Replace("//before", beforeMatch.Success ? beforeMatch.Groups["before"].Value : "return text;")
70+
.Replace("//after", afterMatch.Success ? afterMatch.Groups["after"].Value : "return text;");
7471
}
7572

76-
public object CSharpTextSourceScript
77-
{
78-
get
79-
{
80-
return csEval.LoadCode(Res.CSharpTextSourceContainer
81-
.Replace("//code", cSharpReplaceSpecialZoneCleaningRegex.Replace(TextSourceEditor.Text, string.Empty))
82-
.Replace("//usings", cSharpReplaceUsingsPartRegex.Match(TextSourceEditor.Text).Groups["usings"].Value));
83-
}
84-
}
73+
public string ReplaceScriptForMatch => InjectInReplaceScript(
74+
Res.CSharpReplaceContainer
75+
.RegexReplace(@"\s*//(?<type>group|capture).*//end\k<type>", string.Empty, RegexOptions.Singleline)
76+
.RegexReplace("//match(?<keep>.*)//endmatch", "${keep}", RegexOptions.Singleline));
77+
78+
public string ReplaceScriptForGroup => InjectInReplaceScript(
79+
Res.CSharpReplaceContainer
80+
.RegexReplace(@"\s*//(?<type>match|capture).*//end\k<type>", string.Empty, RegexOptions.Singleline)
81+
.RegexReplace("//group(?<keep>.*)//endgroup", "${keep}", RegexOptions.Singleline));
82+
83+
public string ReplaceScriptForCapture => InjectInReplaceScript(
84+
Res.CSharpReplaceContainer
85+
.RegexReplace(@"\s*//(?<type>match|group).*//end\k<type>", string.Empty, RegexOptions.Singleline)
86+
.RegexReplace("//capture(?<keep>.*)//endcapture", "${keep}", RegexOptions.Singleline));
87+
88+
public string CSharpTextSourceScript => Res.CSharpTextSourceContainer
89+
.Replace("//code", cSharpReplaceSpecialZoneCleaningRegex.Replace(TextSourceEditor.Text, string.Empty))
90+
.Replace("//usings", cSharpReplaceUsingsPartRegex.Match(TextSourceEditor.Text).Groups["usings"].Value);
8591

8692
public delegate bool TryOpenDelegate(string fileName, bool onlyIfAlreadyOpen);
8793
public delegate void SetPositionDelegate(int index, int length);
@@ -622,7 +628,7 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
622628

623629
if (CSharpReplaceCheckbox.IsChecked.GetValueOrDefault())
624630
{
625-
dynamic script = ReplaceScript;
631+
dynamic script = csEval.LoadCode(ReplaceScriptForMatch);
626632

627633
int index = -1;
628634

@@ -697,7 +703,7 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
697703
}), currentFileName, null));
698704
break;
699705
case RegexTextSource.CSharpScript:
700-
dynamic scriptSource = CSharpTextSourceScript;
706+
dynamic scriptSource = csEval.LoadCode(CSharpTextSourceScript);
701707
text = script.Before(scriptSource.Get().ToString(), "script");
702708
nbrOfElementToReplace = regex.Matches(text).Count;
703709
SetTextInNew(script.After(regex.Replace(text, match =>
@@ -773,7 +779,7 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
773779
SetSelectedText(regex.Replace(text, ReplaceEditor.Text));
774780
break;
775781
case RegexTextSource.CSharpScript:
776-
dynamic script = CSharpTextSourceScript;
782+
dynamic script = csEval.LoadCode(CSharpTextSourceScript);
777783
text = script.Get().ToString();
778784
nbrOfElementToReplace = regex.Matches(text).Count;
779785
SetTextInNew(regex.Replace(text, ReplaceEditor.Text));
@@ -852,7 +858,7 @@ private void ExtractMatchesButton_Click(object sender, RoutedEventArgs e)
852858

853859
if (CSharpReplaceCheckbox.IsChecked.GetValueOrDefault())
854860
{
855-
script = ReplaceScript;
861+
script = csEval.LoadCode(ReplaceScriptForMatch);
856862
}
857863

858864
void Extract(string text, string fileName = "")
@@ -1453,7 +1459,7 @@ private void ReplaceInEditor_MenuItem_Click(object sender, RoutedEventArgs e)
14531459

14541460
if (CSharpReplaceCheckbox.IsChecked.GetValueOrDefault())
14551461
{
1456-
dynamic script = ReplaceScript;
1462+
dynamic script = csEval.LoadCode(ReplaceScriptForMatch);
14571463

14581464
int index = -1;
14591465

@@ -1497,14 +1503,12 @@ private void ReplaceInEditor_MenuItem_Click(object sender, RoutedEventArgs e)
14971503

14981504
if (CSharpReplaceCheckbox.IsChecked.GetValueOrDefault())
14991505
{
1500-
dynamic script = ReplaceScript;
1501-
15021506
if (regexResult is RegexMatchResult regexMatchResult)
1503-
newText = beforeMatch + script.Replace((Match)regexMatchResult.RegexElement, regexMatchResult.RegexElementNb, regexResult.FileName, regexMatchResult.RegexElementNb, 0) + afterMatch;
1507+
newText = beforeMatch + ((dynamic)csEval.LoadCode(ReplaceScriptForMatch)).Replace((Match)regexMatchResult.RegexElement, regexMatchResult.RegexElementNb, regexResult.FileName, regexMatchResult.RegexElementNb, 0) + afterMatch;
15041508
else if (regexResult is RegexGroupResult regexGroupResult)
1505-
newText = beforeMatch + script.Replace((Match)regexGroupResult.Parent.RegexElement, (Group)regexGroupResult.RegexElement, regexResult.RegexElementNb, regexResult.FileName, regexResult.RegexElementNb, 0) + afterMatch;
1509+
newText = beforeMatch + ((dynamic)csEval.LoadCode(ReplaceScriptForGroup)).Replace((Match)regexGroupResult.Parent.RegexElement, (Group)regexGroupResult.RegexElement, regexResult.RegexElementNb, regexResult.FileName, regexResult.RegexElementNb, 0) + afterMatch;
15061510
else if (regexResult is RegexCaptureResult regexCaptureResult)
1507-
newText = beforeMatch + script.Replace((Match)regexCaptureResult.Parent.Parent.RegexElement, (Group)regexCaptureResult.Parent.RegexElement, (Capture)regexCaptureResult.RegexElement, regexResult.RegexElementNb, regexResult.FileName, regexResult.RegexElementNb, 0) + afterMatch;
1511+
newText = beforeMatch + ((dynamic)csEval.LoadCode(ReplaceScriptForCapture)).Replace((Match)regexCaptureResult.Parent.Parent.RegexElement, (Group)regexCaptureResult.Parent.RegexElement, (Capture)regexCaptureResult.RegexElement, regexResult.RegexElementNb, regexResult.FileName, regexResult.RegexElementNb, 0) + afterMatch;
15081512
}
15091513
else
15101514
{
@@ -2007,7 +2011,7 @@ private void TestCSharpTextSourceButton_Click(object sender, RoutedEventArgs e)
20072011
{
20082012
try
20092013
{
2010-
dynamic script = CSharpTextSourceScript;
2014+
dynamic script = csEval.LoadCode(CSharpTextSourceScript);
20112015

20122016
string result = script.Get().ToString();
20132017

RegexDialog/Resources/CSharpReplaceContainer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,26 @@ public class CSharpReplaceContainer
1313
{
1414
//global
1515

16+
//match
1617
public string Replace(Match match, int matchIndex, string fileName, int globalIndex, int fileIndex)
1718
{
1819
//code
1920
}
21+
//endmatch
2022

23+
//group
2124
public string Replace(Match match, Group group, int matchIndex, string fileName, int globalIndex, int fileIndex)
2225
{
2326
//code
2427
}
28+
//endgroup
2529

30+
//capture
2631
public string Replace(Match match, Group group, Capture capture, int matchIndex, string fileName, int globalIndex, int fileIndex)
2732
{
2833
//code
2934
}
35+
//endcapture
3036

3137
public string Before(string text, string fileName)
3238
{

RegexDialog/Utils/Extensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,10 @@ public static string UnescapeXml(this string s)
3030

3131
return returnString;
3232
}
33+
34+
public static string RegexReplace(this string input, string pattern, string replacement, RegexOptions options = RegexOptions.None)
35+
{
36+
return Regex.Replace(input, pattern, replacement, options);
37+
}
3338
}
3439
}

0 commit comments

Comments
 (0)