2017-06-12 240 views
0

我需要构造一个只允许a-z,A-Z,0-9,短划线,空格和单引号的正则表达式。字符串内部不允许有双倍空间,短划线只能在字符串内部,双引号不能在字符串内部允许。该字符串只能以字母(如果可能,最好大写)或数字(0-9)开头。 有什么建议吗?正则表达式与字母数字,空格和破折号

允许:

"My Test" 
    "My-test" 
    "1My-t-es-t" 
    "1250 My t-es-t" 

不允许:

"My Test" 
    "-My Test-" 
    "My T''est" 
+0

你试过了什么?这里没有人会为你写代码。 –

回答

1

这可能会实现
https://regex101.com/r/h8ggbH/1

"[A-Z0-9](?:[a-zA-Z0-9]|[ ](?![ ])|'(?!')|-(?!"))*"

解释

"      # Dbl Quote 
[A-Z0-9]    # UC Letter or digit 
(?:     # Cluster 
     [a-zA-Z0-9]   # Alphanum 
    |      # or, 
     [ ]     # Space 
     (?! [ ])    # if not followed by space 
    |      # or, 
     '      # Quote 
     (?! ')    # if not followed by quote 
    |      # or, 
     -      # Dash 
     (?! ")    # if not followed by dbl quote 
)*     # Do 0 to many times 
"      # Dbl Quote 
相关问题