How to regex match a string of alnums and hyphens, but which doesn't begin or end with a hyphen?
I have some code validating a string of 1 to 32 characters, which may contain only alpha-numerics and hyphens ('-') but may not begin or end with a hyphen.
I'm using PCRE regular expressions & PHP (albeit th开发者_StackOverflowe PHP part is not really important in this case).
Right now the pseudo-code looks like this:
if (match("/^[\p{L}0-9][\p{L}0-9-]{0,31}$/u", string)
and
not match("/-$/", string))
print "success!"
That is, I'm checking first that the string is of right contents, doesn't being with a '-' and is of the right length, and then I'm running another test to see that it doesn't end with a '-'.
Any suggestions on merging this into a single PCRE regular expression?
I've tried using look-ahead / look-behind assertions but couldn't get it to work.
Try this regular expression:
/^[\p{L}0-9](?:[\p{L}0-9-]{0,30}[\p{L}0-9])?$/u
And if you want to use look-around assertions:
/^[\p{L}0-9][\p{L}0-9-]{0,31}$(?<!-)/u
A slightly alternative approach would be to keep your character class in one piece and be specific about the points where you don't want to allow the hyphen.
/^(?!-)[\p{L}0-9-]{1,32}(?<!-)$/Du
Also note the D
modifier which everyone always seems to forget.
Finally, just to be sure, you are aware that \pL
will match much more than a-zA-Z
, right? Just checking.
精彩评论