2017-06-12 181 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 
相關問題