Regular expression: numbers spaces parentheses +
I need a regular expression wh开发者_如何学JAVAere any number is allowed with spaces, parentheses and hyphens in any order. But there must be a "+" (plus sign) at the end.
You can use the regex:
^[\d() -]+\+$
Explanation:
^ : Start anchor
[ : Start of char class.
\d : Any digit
( : A literal (. No need to escape it as it is non-special inside char class.
) : A literal )
: A space
- : A hyphen. To list a literal hyphen in char class place it at the beginning
or at the end without escaping it or escape it and place it anywhere.
] : End of char class
+ : One or more of the char listed in the char class.
\+ : A literal +. Since a + is metacharacter we need to escape it.
$ : End anchor
If the rules mean that the whole string must be according to them, then:
/^[\d\(\)\- ]+\+$/
This will match (i) 435 (345-325) +
but not (ii) my phone is 435 (345-325)+, remember it
.
If you want to just extract (i) from (ii) you could use my original RegExp:
/[\d\(\)\- ]+\+/
精彩评论