Regexp.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /*
  3. * $Id: Regexp.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. /**
  22. * A factory class for regex functions.
  23. * @author Hans Lellelid <hans@xmpl.org>
  24. * @package phing.util.regexp
  25. * @version $Revision: 905 $
  26. */
  27. class Regexp {
  28. /**
  29. * Matching groups found.
  30. * @var array
  31. */
  32. private $groups = array();
  33. /**
  34. * Pattern to match.
  35. * @var string
  36. */
  37. private $pattern;
  38. /**
  39. * Replacement pattern.
  40. * @var string
  41. */
  42. private $replace;
  43. /**
  44. * The regex engine -- e.g. 'preg' or 'ereg';
  45. * @var RegexpEngine
  46. */
  47. private $engine;
  48. /**
  49. * Constructor sets the regex engine to use (preg by default).
  50. * @param string $_engineType The regex engine to use.
  51. */
  52. function __construct($engineType='preg') {
  53. if ($engineType == 'preg') {
  54. include_once 'phing/util/regexp/PregEngine.php';
  55. $this->engine = new PregEngine();
  56. } elseif ($engineType == 'ereg') {
  57. include_once 'phing/util/regexp/EregEngine.php';
  58. $this->engine = new EregEngine();
  59. } else {
  60. throw new BuildException("Invalid engine type for Regexp: " . $engineType);
  61. }
  62. }
  63. /**
  64. * Sets pattern to use for matching.
  65. * @param string $pat The pattern to match on.
  66. * @return void
  67. */
  68. public function setPattern($pat) {
  69. $this->pattern = (string) $pat;
  70. }
  71. /**
  72. * Gets pattern to use for matching.
  73. * @return string The pattern to match on.
  74. */
  75. public function getPattern() {
  76. return $this->pattern;
  77. }
  78. /**
  79. * Sets replacement string.
  80. * @param string $rep The pattern to replace matches with.
  81. * @return void
  82. */
  83. public function setReplace($rep) {
  84. $this->replace = (string) $rep;
  85. }
  86. /**
  87. * Gets replacement string.
  88. * @return string The pattern to replace matches with.
  89. * @return void
  90. */
  91. public function getReplace() {
  92. return $this->replace;
  93. }
  94. /**
  95. * Performs match of specified pattern against $subject.
  96. * @param string $subject The subject, on which to perform matches.
  97. * @return boolean Whether or not pattern matches subject string passed.
  98. */
  99. public function matches($subject) {
  100. if($this->pattern === null) {
  101. throw new Exception("No pattern specified for regexp match().");
  102. }
  103. return $this->engine->match($this->pattern, $subject, $this->groups);
  104. }
  105. /**
  106. * Performs replacement of specified pattern and replacement strings.
  107. * @param string $subject Text on which to perform replacement.
  108. * @return string subject after replacement has been performed.
  109. */
  110. public function replace($subject) {
  111. if ($this->pattern === null || $this->replace === null) {
  112. throw new Exception("Missing pattern or replacement string regexp replace().");
  113. }
  114. return $this->engine->replace($this->pattern, $this->replace, $subject);
  115. }
  116. /**
  117. * Get array of matched groups.
  118. * @return array Matched groups
  119. */
  120. function getGroups() {
  121. return $this->groups;
  122. }
  123. /**
  124. * Get specific matched group.
  125. * @param integer $idx
  126. * @return string specified group or NULL if group is not set.
  127. */
  128. function getGroup($idx) {
  129. if (!isset($this->groups[$idx])) {
  130. return null;
  131. }
  132. return $this->groups[$idx];
  133. }
  134. /**
  135. * Sets pattern modifiers for regex engine
  136. *
  137. * @param string $mods Modifiers to be applied to a given regex
  138. * @return void
  139. */
  140. public function setModifiers($mods) {
  141. $this->engine->setModifiers($mods);
  142. }
  143. /**
  144. * Gets pattern modifiers.
  145. * Subsequent call to engines getModifiers() filters out duplicates
  146. * i.e. if i is provided in $mods, and setIgnoreCase(true), "i"
  147. * modifier would be included only once
  148. * @return string
  149. */
  150. public function getModifiers() {
  151. return $this->engine->getModifiers();
  152. }
  153. /**
  154. * Sets whether the regexp matching is case insensitive.
  155. * (default is false -- i.e. case sensisitive)
  156. * @param boolean $bit
  157. */
  158. function setIgnoreCase($bit) {
  159. $this->engine->setIgnoreCase($bit);
  160. }
  161. /**
  162. * Gets whether the regexp matching is case insensitive.
  163. * @return boolean
  164. */
  165. function getIgnoreCase() {
  166. return $this->engine->getIgnoreCase();
  167. }
  168. /**
  169. * Sets whether regexp should be applied in multiline mode.
  170. * @param boolean $bit
  171. */
  172. function setMultiline($bit) {
  173. $this->engine->setMultiline($bit);
  174. }
  175. /**
  176. * Gets whether regexp is to be applied in multiline mode.
  177. * @return boolean
  178. */
  179. function getMultiline() {
  180. return $this->engine->getMultiline();
  181. }
  182. }