@@ -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
0 commit comments