Reputation: 168
To make my code more readable, and to add more functions where a particular regular expression is needed.
/**
* Regex, Matching when one of the listed exenstion is on a string.
* @see https://nl.wikipedia.org/wiki/Pantone
* @link https://regex101.com/r/fK8jQ2/2
* @var string
*/
CONST PATTERN_PANTONE_EXTENSION = "/((.*)(\s(C|U|M|TC|TP|T|CV|CVC|CVU|CVP|SWOP|SWOP-Euro|DS|S)))$/";
The code i'm using:
if(preg_match(PATTERN_PANTONE_EXTENSION, $colorCode, $matches)) {
return $matches[2];
}
When i'm using this, the following error will occur.
preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Delimiter must not be alphanumeric or backslash
Is it possible to store a regular expression inside a constant?
Upvotes: 0
Views: 1366
Reputation: 300
If this is truly a class const, then you are not referencing the constant correctly. You need to include the class name and Scope Resolution Operator like so: Test::PATTERN_PANTONE_EXTENSION
.
I imagine a notice is being thrown about an undefined constant and your error level is not set high enough to show this. After that it tries to use the literal string "PATTERN_PANTONE_EXTENSION" as a regex, with a delimiter of "P", which obviously is not going to work.
Complete example:
class Test {
/**
* Regex, Matching when one of the listed exenstion is on a string.
* @see https://nl.wikipedia.org/wiki/Pantone
* @link https://regex101.com/r/fK8jQ2/2
* @var string
*/
const PATTERN_PANTONE_EXTENSION = "/((.*)(\s(C|U|M|TC|TP|T|CV|CVC|CVU|CVP|SWOP|SWOP-Euro|DS|S)))$/";
}
if(preg_match(Test::PATTERN_PANTONE_EXTENSION, $colorCode, $matches)) {
return $matches[2];
}
Upvotes: 0
Reputation: 32747
You need to use self::
to access a class const.
so try
if(preg_match(self::PATTERN_PANTONE_EXTENSION, $colorCode, $matches)) {
return $matches[2];
}
Upvotes: 2