Reputation: 289
I am using preg_match in PHP together with a REGEX to change the path variable of the string containing:
"any character - paint, old number between one and fourteen - any character"
I put examples of valid strings:
-paint, old 12
-paint, old 0a
-paint, old 6b
...
I have used the preg_match as follows:
if(preg_match("/*paint, old ^([1-9]|1[0-4])*/",$ubicacion))
{
...
}
else
{
...
}
but it gives me this error: preg_match (): Compilation failed: nothing to repeat at offset 0
Do you know what is failing?
Upvotes: -1
Views: 92
Reputation: 627101
The preg_match (): Compilation failed: nothing to repeat at offset 0
in most cases is due to the presence of a quantifier at the start of the regex. A regex cannot start with a quantifier, i.e. /+1/
, /*1/
, /{2,}1/
and /{2,5}1/
will throw this error.
You can use
if (preg_match('~\bpaint,\s+old\s+(1[0-4]|[1-9])(?!\d)~i', $string)) {
...
}
See the regex demo and this PHP demo. Details:
\b
- a word boundarypaint,
- paint,
string\s+
- one or more whitespaces
-old
- an old
string\s+
- one or more whitespaces(1[0-4]|[1-9])
- 1
followed with a digit from 0
to 4
or a non-zero digit(?!\d)
- not followed with any other digit.Note you do not need to add any patterns at the start and end to actually consume the texts before and after your expected match since all you need is a boolean result.
The i
at the end makes the pattern match in a case insensitive way.
Upvotes: 1