Regex match only if character is so long

Blah having one of those days. Using preg_match how can I test a string to only match if it has so many characters or less?

Here is an example string:



I dont want the match to work above because test is 4 characters long, I just want it to match if the string before - is 3 characters or less. And yes I could use explode but there is more to the code that requires preg_match. when I do the test above with the example string it gives me back:


Ah, I see.

You could use a ‘boundary’.


The string may be farther down in the whole string itself. For example:

This would be an example string that-shows what your doing and-this shows what you arent doing.

I want to match and-this only because it has 3 characters before the -.

Ah, you nearly had it. :smiley:


Just anchor the pattern at the beginning of the string. :wink: