正则表达式:数字空格括号+

Mar*_*cus 3 regex

我需要一个正则表达式,其中任何数字都允许使用任何顺序的空格,括号和连字符.但最后必须有一个"+"(加号).

cod*_*ict 14

你可以使用正则表达式:

^[\d() -]+\+$
Run Code Online (Sandbox Code Playgroud)

说明:

^   : 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
Run Code Online (Sandbox Code Playgroud)

  • 优秀的答案和解释.如果我可以两次投票,我会的. (2认同)