@@ -57,31 +57,37 @@ public partial class RegExToolDialog : Window
57
57
private static readonly Regex cSharpReplaceBeforePartRegex = new Regex ( @"(?<=^|\s)\#before(?=\s)(?<before>.*)(?<=\s)\#endbefore(?=\s|$)" , RegexOptions . Singleline | RegexOptions . Compiled ) ;
58
58
private static readonly Regex cSharpReplaceAfterPartRegex = new Regex ( @"(?<=^|\s)\#after(?=\s)(?<after>.*)(?<=\s)\#endafter(?=\s|$)" , RegexOptions . Singleline | RegexOptions . Compiled ) ;
59
59
60
- public object ReplaceScript
60
+ private string InjectInReplaceScript ( string replaceScript )
61
61
{
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 ) ;
66
64
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;" ) ;
74
71
}
75
72
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 ) ;
85
91
86
92
public delegate bool TryOpenDelegate ( string fileName , bool onlyIfAlreadyOpen ) ;
87
93
public delegate void SetPositionDelegate ( int index , int length ) ;
@@ -622,7 +628,7 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
622
628
623
629
if ( CSharpReplaceCheckbox . IsChecked . GetValueOrDefault ( ) )
624
630
{
625
- dynamic script = ReplaceScript ;
631
+ dynamic script = csEval . LoadCode ( ReplaceScriptForMatch ) ;
626
632
627
633
int index = - 1 ;
628
634
@@ -697,7 +703,7 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
697
703
} ) , currentFileName , null ) ) ;
698
704
break ;
699
705
case RegexTextSource . CSharpScript :
700
- dynamic scriptSource = CSharpTextSourceScript ;
706
+ dynamic scriptSource = csEval . LoadCode ( CSharpTextSourceScript ) ;
701
707
text = script . Before ( scriptSource . Get ( ) . ToString ( ) , "script" ) ;
702
708
nbrOfElementToReplace = regex . Matches ( text ) . Count ;
703
709
SetTextInNew ( script . After ( regex . Replace ( text , match =>
@@ -773,7 +779,7 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
773
779
SetSelectedText ( regex . Replace ( text , ReplaceEditor . Text ) ) ;
774
780
break ;
775
781
case RegexTextSource . CSharpScript :
776
- dynamic script = CSharpTextSourceScript ;
782
+ dynamic script = csEval . LoadCode ( CSharpTextSourceScript ) ;
777
783
text = script . Get ( ) . ToString ( ) ;
778
784
nbrOfElementToReplace = regex . Matches ( text ) . Count ;
779
785
SetTextInNew ( regex . Replace ( text , ReplaceEditor . Text ) ) ;
@@ -852,7 +858,7 @@ private void ExtractMatchesButton_Click(object sender, RoutedEventArgs e)
852
858
853
859
if ( CSharpReplaceCheckbox . IsChecked . GetValueOrDefault ( ) )
854
860
{
855
- script = ReplaceScript ;
861
+ script = csEval . LoadCode ( ReplaceScriptForMatch ) ;
856
862
}
857
863
858
864
void Extract ( string text , string fileName = "" )
@@ -1453,7 +1459,7 @@ private void ReplaceInEditor_MenuItem_Click(object sender, RoutedEventArgs e)
1453
1459
1454
1460
if ( CSharpReplaceCheckbox . IsChecked . GetValueOrDefault ( ) )
1455
1461
{
1456
- dynamic script = ReplaceScript ;
1462
+ dynamic script = csEval . LoadCode ( ReplaceScriptForMatch ) ;
1457
1463
1458
1464
int index = - 1 ;
1459
1465
@@ -1497,14 +1503,12 @@ private void ReplaceInEditor_MenuItem_Click(object sender, RoutedEventArgs e)
1497
1503
1498
1504
if ( CSharpReplaceCheckbox . IsChecked . GetValueOrDefault ( ) )
1499
1505
{
1500
- dynamic script = ReplaceScript ;
1501
-
1502
1506
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 ;
1504
1508
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 ;
1506
1510
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 ;
1508
1512
}
1509
1513
else
1510
1514
{
@@ -2007,7 +2011,7 @@ private void TestCSharpTextSourceButton_Click(object sender, RoutedEventArgs e)
2007
2011
{
2008
2012
try
2009
2013
{
2010
- dynamic script = CSharpTextSourceScript ;
2014
+ dynamic script = csEval . LoadCode ( CSharpTextSourceScript ) ;
2011
2015
2012
2016
string result = script . Get ( ) . ToString ( ) ;
2013
2017
0 commit comments