I’m using the following regex code:
^[a-z0-9_-]{3,15}$^
I’m using this for username validation and I want it to match alphanumeric characters, - , _ and periods.
The following weird thing happens:
It doesn’t match this:
bla.b
But it matches this one:
bla.blabla
How can I change this, so that it matches both? I still would like to be able to change the min and max characters freely. (btw. there maybe more wrong things about this regex. This one I discovered accidentally)
UPDATE: I should mention that I’m using this in CakePHP validation and this gives me an error:
^[a-z0-9_.-]{3,15}$
this is the error:
Warning (2): preg_match() [function.preg-match]: No ending delimiter '^' found
You made a little mistake and forgot to put the
^at the beginning. Choosing a different delimiter might make that more visible:Remember:
^– marks the beginning of the subject$– marks the end of the subjectBoth are part of the pattern. The delimiters are used to separate the pattern from the modifiers (you don’t use any modifiers here).
Alternatively you can denote the beginning and end as well with
\Aand\Zif it helps.To now also match the dot, add it to your character class: