replace.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // assembly: System
  3. // namespace: System.Text.RegularExpressions
  4. // file: replace.cs
  5. //
  6. // author: Dan Lewis ([email protected])
  7. // (c) 2002
  8. using System;
  9. using System.Text;
  10. using System.Collections;
  11. using Parser = System.Text.RegularExpressions.Syntax.Parser;
  12. namespace System.Text.RegularExpressions {
  13. class ReplacementEvaluator {
  14. public static string Evaluate (string replacement, Match match) {
  15. ReplacementEvaluator ev = new ReplacementEvaluator (match.Regex, replacement);
  16. return ev.Evaluate (match);
  17. }
  18. public ReplacementEvaluator (Regex regex, string replacement) {
  19. this.regex = regex;
  20. terms = new ArrayList ();
  21. Compile (replacement);
  22. }
  23. public string Evaluate (Match match) {
  24. StringBuilder result = new StringBuilder ();
  25. foreach (Term term in terms)
  26. result.Append (term.GetResult (match));
  27. return result.ToString ();
  28. }
  29. // private
  30. private void Compile (string replacement) {
  31. replacement = Parser.Unescape (replacement);
  32. string literal = "";
  33. int ptr = 0;
  34. char c;
  35. Term term = null;
  36. while (ptr < replacement.Length) {
  37. c = replacement[ptr ++];
  38. if (c == '$') {
  39. if (replacement[ptr] == '$') {
  40. ++ ptr;
  41. break;
  42. }
  43. term = CompileTerm (replacement, ref ptr);
  44. }
  45. if (term != null) {
  46. term.Literal = literal;
  47. terms.Add (term);
  48. term = null;
  49. literal = "";
  50. }
  51. else
  52. literal += c;
  53. }
  54. if (term == null && literal.Length > 0) {
  55. terms.Add (new Term (literal));
  56. }
  57. }
  58. private Term CompileTerm (string str, ref int ptr) {
  59. char c = str[ptr];
  60. if (Char.IsDigit (c)) { // numbered group
  61. int n = Parser.ParseDecimal (str, ref ptr);
  62. if (n < 0 || n > regex.GroupCount)
  63. throw new ArgumentException ("Bad group number.");
  64. return new Term (TermOp.Match, n);
  65. }
  66. ++ ptr;
  67. switch (c) {
  68. case '{': { // named group
  69. string name = Parser.ParseName (str, ref ptr);
  70. if (str[ptr ++] != '}' || name == null)
  71. throw new ArgumentException ("Bad group name.");
  72. int n = regex.GroupNumberFromName (name);
  73. if (n < 0)
  74. throw new ArgumentException ("Bad group name.");
  75. return new Term (TermOp.Match, n);
  76. }
  77. case '&': // entire match
  78. return new Term (TermOp.Match, 0);
  79. case '`': // text before match
  80. return new Term (TermOp.PreMatch, 0);
  81. case '\'': // text after match
  82. return new Term (TermOp.PostMatch, 0);
  83. case '+': // last group
  84. return new Term (TermOp.Match, regex.GroupCount - 1);
  85. case '_': // entire text
  86. return new Term (TermOp.All, 0);
  87. default:
  88. throw new ArgumentException ("Bad replacement pattern.");
  89. }
  90. }
  91. private Regex regex;
  92. private ArrayList terms;
  93. private enum TermOp {
  94. None, // no action
  95. Match, // input within group
  96. PreMatch, // input before group
  97. PostMatch, // input after group
  98. All // entire input
  99. }
  100. private class Term {
  101. public Term (TermOp op, int arg) {
  102. this.op = op;
  103. this.arg = arg;
  104. this.literal = "";
  105. }
  106. public Term (string literal) {
  107. this.op = TermOp.None;
  108. this.arg = 0;
  109. this.literal = literal;
  110. }
  111. public string Literal {
  112. set { literal = value; }
  113. }
  114. public string GetResult (Match match) {
  115. Group group = match.Groups[arg];
  116. switch (op) {
  117. case TermOp.None:
  118. return literal;
  119. case TermOp.Match:
  120. return literal + group.Value;
  121. case TermOp.PreMatch:
  122. return literal + group.Text.Substring (0, group.Index);
  123. case TermOp.PostMatch:
  124. return literal + group.Text.Substring (group.Index + group.Length);
  125. case TermOp.All:
  126. return literal + group.Text;
  127. }
  128. return "";
  129. }
  130. public TermOp op; // term type
  131. public int arg; // group argument
  132. public string literal; // literal to prepend
  133. public override string ToString () {
  134. return op.ToString () + "(" + arg + ") " + literal;
  135. }
  136. }
  137. }
  138. }