Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3769425
D110.id265.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D110.id265.diff
View Options
diff --git a/lists/RegexpFactory.php b/lists/RegexpFactory.php
--- a/lists/RegexpFactory.php
+++ b/lists/RegexpFactory.php
@@ -5,10 +5,16 @@
*/
class RegexpFactory {
/**
- * The regular expression
- * @var string
+ * The regular expressions
+ * @var string[]
+ */
+ public $expressions;
+
+ /**
+ * The replacement expressions
+ * @var string[]
*/
- public $expression;
+ public $replaceExpressions;
/**
* The last error
@@ -17,49 +23,91 @@
public $lastError;
/**
+ * Regular expression to delimit regexps/replaces
+ */
+ const DELIMITER = '/\r\n|\n|\r/';
+
+ /**
* Initializes a new instance of the RegexpFactory object.
*
- * @param string The regular expression
+ * @param string $rexpressions The regular expression
+ * @param string $replaceExpressions The format of the result string
*/
- function __construct ($expression) {
- $this->expression = $expression;
+ function __construct ($expressions, $replaceExpressions) {
+ $this->expressions = preg_split(static::DELIMITER, $expressions);
+ $this->replaceExpressions = preg_split(static::DELIMITER, $replaceExpressions);
+
+ if (count($this->expressions) != count($this->replaceExpressions)) {
+ throw new Exception("The number of expressions and replacements should match.");
+ }
}
/**
* Replaces an expression using regexp, a similar way Apache mod_rewrite and Nginx wreplace do.
*
* @param string $haystack The expression to perform a replacement on
- * @param string $replaceExpression The format of the result string
* @return string The replaced string
*/
- function replace ($haystack, $replaceExpression) {
- return preg_replace($this->expression, $replaceExpression, $haystack);
+ function replace ($haystack) {
+ $text = $haystack;
+ for ($i = 0 ; $i < count($this->expressions) ; $i++) {
+ $expression = $this->expressions[$i];
+ $replaceExpression = $this->replaceExpressions[$i];
+ $text = preg_replace($expression, $replaceExpression, $text);
+ }
+ return $text;
+ }
+
+ /**
+ * Adds delimiters to each regular expression.
+ */
+ public function addDelimiters () {
+ array_walk($this->expressions, function (&$item) {
+ $item = RegexpFactory::addDelimitersToExpression($item);
+ });
}
/**
- * Encloses the regular expression with delimiters.
+ * Encloses the specified regular expression with delimiters.
+ *
+ * @param string $expression The expression to delimit
+ * @return string The expression with delimiters
*/
- function addDelimiters () {
+ public static function addDelimitersToExpression ($expression) {
$delimiters = ['/', '@', '#', '~', '+', '%', '♦', 'µ', '±', '☞'];
//TODO: check if it's okay to use UTF-8 characters as delimiters
foreach ($delimiters as $delimiter) {
- if (strpos($this->expression, $delimiter) === false) {
- $this->expression = $delimiter . $this->expression . $delimiter;
- return;
+ if (strpos($expression, $delimiter) === false) {
+ return $delimiter . $expression . $delimiter;
+ }
+ }
+ throw new Exception("Can't delimite regexp $expression");
+ }
+
+ /**
+ * Determines if the specified expression block is valid.
+ *
+ * @return bool true if each expression is valid; otherwise, false.
+ */
+ public function isValid () {
+ foreach ($this->expressions as $expression) {
+ if (!$this->isValidExpression($expression)) {
+ return false;
}
}
- throw new Exception("Can't delimite regexp $this->expression");
+ return true;
}
/**
* Determines if the specified expression is valid.
*
+ * @param string the regexp to test
* @return bool true if the expression is valid; otherwise, false.
*/
- public function isValid () {
+ public function isValidExpression ($expression) {
$this->lastError = '';
set_error_handler('self::handleErrors');
- $result = preg_match($this->expression, null);
+ $result = preg_match($expression, null);
restore_error_handler();
if ($this->lastError === '' && $result === false) {
$this->lastError = self::getPCREError();
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Nov 24, 14:34 (6 h, 4 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2258961
Default Alt Text
D110.id265.diff (4 KB)
Attached To
Mode
D110: list/replace: multi regexp support
Attached
Detach File
Event Timeline
Log In to Comment