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:

test-string


preg_match('#[a-z]{1,3}\\-[a-z]+$#i',$string,$output);

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:

est-string

Ah, I see.

You could use a ‘boundary’.


~\\b([a-z]{1,3}-[a-z]+)~i

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:


preg_match('#$[a-z]{1,3}\\-[a-z]+$#i',$string,$output);

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

Anthony.