EReg.hx 3.5 KB

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