开发者

Php Regular expression [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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}$/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜