2
0

EReg.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import java.util.regex.Regex;
  2. /*
  3. * Copyright (c) 2005, The haXe Project Contributors
  4. * All rights reserved.
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * - Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  18. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  24. * DAMAGE.
  25. */
  26. /**
  27. Regular expressions are a way to find regular patterns into
  28. Strings. Have a look at the tutorial on haXe website to learn
  29. how to use them.
  30. **/
  31. class EReg {
  32. private var pattern:String;
  33. private var matcher:Matcher;
  34. private var cur:String;
  35. /**
  36. Creates a new regular expression with pattern [r] and
  37. options [opt].
  38. **/
  39. public function new( r : String, opt : String ) {
  40. //FIXME opt is ignored by now
  41. matcher = Pattern.compile(r).matcher("");
  42. pattern = r;
  43. }
  44. /**
  45. Tells if the regular expression matches the String.
  46. Updates the internal state accordingly.
  47. **/
  48. public function match( s : String ) : Bool {
  49. cur = s;
  50. matcher = matcher.reset(s);
  51. return matcher.find();
  52. }
  53. /**
  54. Returns a matched group or throw an expection if there
  55. is no such group. If [n = 0], the whole matched substring
  56. is returned.
  57. **/
  58. public function matched( n : Int ) : String
  59. {
  60. return matcher.group(n);
  61. }
  62. /**
  63. Returns the part of the string that was as the left of
  64. of the matched substring.
  65. **/
  66. public function matchedLeft() : String
  67. {
  68. return cur.substr(0, matcher.start());
  69. }
  70. /**
  71. Returns the part of the string that was at the right of
  72. of the matched substring.
  73. **/
  74. public function matchedRight() : String
  75. {
  76. return cur.substr(matcher.end());
  77. }
  78. /**
  79. Returns the position of the matched substring within the
  80. original matched string.
  81. **/
  82. public function matchedPos() : { pos : Int, len : Int } {
  83. var start = matcher.start();
  84. return { pos : start, len : matcher.end() - start };
  85. }
  86. /**
  87. Split a string by using the regular expression to match
  88. the separators.
  89. **/
  90. @:functionBody('
  91. return new Array<String>(s.split(this.pattern));
  92. ')
  93. public function split( s : String ) : Array<String>
  94. {
  95. return null;
  96. }
  97. /**
  98. Replaces a pattern by another string. The [by] format can
  99. contains [$1] to [$9] that will correspond to groups matched
  100. while replacing. [$$] means the [$] character.
  101. **/
  102. public function replace( s : String, by : String ) : String {
  103. matcher.reset(s);
  104. return matcher.replaceAll(by);
  105. }
  106. /**
  107. For each occurence of the pattern in the string [s], the function [f] is called and
  108. can return the string that needs to be replaced. All occurences are matched anyway,
  109. and setting the [g] flag might cause some incorrect behavior on some platforms.
  110. **/
  111. public function customReplace( s : String, f : EReg -> String ) : String {
  112. var buf = new StringBuf();
  113. while( true ) {
  114. if( !match(s) )
  115. break;
  116. buf.add(matchedLeft());
  117. buf.add(f(this));
  118. s = matchedRight();
  119. }
  120. buf.add(s);
  121. return buf.toString();
  122. }
  123. }