fork download
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var s = "The $quick$ brown '$$fox$$' jumps $$over$$ the '$lazy$' dog";
  10. var p = @"(?x)
  11. (?<quote>'[^'\\]*(?:\\.[^\\']*)*') # A single quoted string literal pattern
  12. | # or
  13. (?<!\$) # no $ immediately to the left
  14. (\${1,2}) # 1 or 2 $ symbols (Group 1)
  15. [^$]+ # 1 or more non-$ chars
  16. \1 # Same value as in Group 1 (backreference)
  17. (?!\$) # No $ immediately to the left of the current location
  18. ";
  19. var result = Regex.Replace(s, p, m =>
  20. m.Groups["quote"].Success ? m.Groups["quote"].Value : "substituted");
  21. Console.WriteLine(result);
  22. }
  23. }
Success #stdin #stdout 0.04s 133824KB
stdin
Standard input is empty
stdout
The substituted brown '$$fox$$' jumps substituted the '$lazy$' dog