sniper sniper - 8 months ago 49
PHP Question

preg_match matching pattern with asterisk (*) in it

preg_match('/te**ed/i', 'tested', $matches);

gives me the following error:

ERROR: nothing to repeat at offset 3

What do I do to allow the pattern to actually contain


To use literal asterisks you have to escape them with backslashes. To match the literal te**ed you would use an expression like this:

preg_match('/te\*\*ed/i', 'tested', $matches); // no match (te**ed != tested)

But I doubt this is what you wanted. If you mean, match any character, you need to use .:

preg_match('/te..ed/is', 'tested', $matches); // match

If you really want any two lower case letter, then this expression:

preg_match('/te[a-z]{2}ed/i', 'tested', $matches); // match