Php Regular expression [closed]
I need a regular expression that accept numb开发者_StackOverflow社区ers, spaces and hyphen with a maximum of 8 caracters. Example 2632-632, 3636 252
Thanks
Use this regex:
~^[\d\s-]{0,8}$~
Here is a regular expression that accept numbers, spaces and hyphen with a maximum of 8 caracters.
/^[\d -]{1,8}$/D
The question is, perhaps unknowingly, ambiguous so some liberties have been taken with the details.
"Spaces" is taken to mean the horizontal space character (ASCII 32); a minimum of 1 character is required (empty strings don't match); and contrary to the other answers a trailing newline character will not be accepted (thanks to the D
pattern modifier).
/^([0-9 \-]){0,8}$/
ought to work, though I haven't tested so forgive me if I have made a silly error.
Try this:
/^(\d|\s|-){0,8}$/
Here you go: /^[\d -]{0,8}$/
Edit: Since I was so flippant before, this regex now does exactly what you want: A string with digits (\d
), spaces (), and hyphens (
-
) between 0 and 8 characters long. \s
is for "whitespace character", which you didn't specify in the question, thus the change to .
If it must be exactly 8 chars:
/^[\d\s-]{8}$/
精彩评论