2016-07-12 78 views
-3

我有此正則表達式:正則表達式:以匹配某些字符,但沒有空間允許

(.*^(?=.{16,25}$)(?=.*[a-z]{1,})(?=.*[A-Z]{1,})(?=.*[0-9]{1,})(?=.*\W{1,8}).*$) 

要匹配的密碼(字符串)上:

  • 16-25長度
  • 1對多AZ
  • 1對多AZ
  • 1對多0-9
  • 1對多符號

但我不想\s,我不知道如何把它放在正則表達式中。

有什麼建議嗎?

我編輯原始限制字符{1,8}到{1,}

+1

確定的模式在所有工作?你的意思是要求1至8 *連續*低,大寫字母,數字和符號?開頭的'。*'使得長度檢查超前無效。嘗試[^(?= \ S {16,25} $)(?=(?:[^ az] * [az]){1,8})(?=(?:[^ AZ] * [AZ ]){1,8})(=(?:?\ d * \ d){1,8})(=(?:?\ W * \ W){1,8})\ S * $'] (https://regex101.com/r/fH8mF7/1),允許在字符串中的任何位置使用1到8個字符。 '\ S'應該用來禁止空格。 –

+1

僅僅說出16-25個非空白字符而不是隨意地聲明第一個1-8必須是小寫字母,下一個大寫字母,然後是數字和符號,會不會更簡單和(可能)更安全?那麼你只需要'/^[^ \ s] {16,25} $ /':https://xkcd.com/936/ – CD001

+1

'/^[^ \ s] {16,25} $ /'將允許一個密碼充滿'a' –

回答

2

說明
^(?=(?:[^a-z]*?[a-z]){1,8}(?!.*?[a-z]))(?=(?:[^0-9]*?[0-9]){1,8}(?!.*?[0-9]))(?=(?:[^A-Z]*?[A-Z]){1,8}(?!.*?[A-Z]))(?=(?:[^!-\/:[email protected][-`{-~]*?[!-\/:[email protected][-`{-~]){1,8}(?!.*?[!-\/:[email protected][-`{-~]))[a-zA-Z0-9!-\/:[email protected][-`{-~]{16,25}$ 

Regular expression visualization

**要查看該圖像更好,只需右鍵單擊該圖像並選擇視圖

這個正則表達式將執行以下操作:

  • 需要字符串爲16-25長度
  • 要求1至8中的z字符在字符串的任何地方和沒有更多
  • 要求1至8個AZ字符字符串並在字符串中沒有更多的
  • 要求1至8中0-9個字符的任何地方和沒有更多
  • 所需要的字符串中的任何1至8個符號和不多
  • 允許零屬中的任何地方尖子

現場演示

https://regex101.com/r/oS4mY2/2

示例文本

注:第一個例子是從你的commment,但它包含更多的低於8大寫字母,因此它不匹配。

aWoeed1#fde39393aii 
aWoeed1#fde39393AII 

樣品匹配

aWoeed1#fde39393AII 

說明

NODE      EXPLANATION 
-------------------------------------------------------------------------------- 
^      the beginning of the string 
-------------------------------------------------------------------------------- 
    (?=      look ahead to see if there is: 
-------------------------------------------------------------------------------- 
    (?:      group, but do not capture (between 1 and 
          8 times (matching the most amount 
          possible)): 
-------------------------------------------------------------------------------- 
     [^a-z]*?     any character except: 'a' to 'z' (0 or 
           more times (matching the least amount 
           possible)) 
-------------------------------------------------------------------------------- 
     [a-z]     any character of: 'a' to 'z' 
-------------------------------------------------------------------------------- 
    ){1,8}     end of grouping 
-------------------------------------------------------------------------------- 
    (?!      look ahead to see if there is not: 
-------------------------------------------------------------------------------- 
     .*?      any character except \n (0 or more 
           times (matching the least amount 
           possible)) 
-------------------------------------------------------------------------------- 
     [a-z]     any character of: 'a' to 'z' 
-------------------------------------------------------------------------------- 
    )      end of look-ahead 
-------------------------------------------------------------------------------- 
)      end of look-ahead 
-------------------------------------------------------------------------------- 
    (?=      look ahead to see if there is: 
-------------------------------------------------------------------------------- 
    (?:      group, but do not capture (between 1 and 
          8 times (matching the most amount 
          possible)): 
-------------------------------------------------------------------------------- 
     [^0-9]*?     any character except: '0' to '9' (0 or 
           more times (matching the least amount 
           possible)) 
-------------------------------------------------------------------------------- 
     [0-9]     any character of: '0' to '9' 
-------------------------------------------------------------------------------- 
    ){1,8}     end of grouping 
-------------------------------------------------------------------------------- 
    (?!      look ahead to see if there is not: 
-------------------------------------------------------------------------------- 
     .*?      any character except \n (0 or more 
           times (matching the least amount 
           possible)) 
-------------------------------------------------------------------------------- 
     [0-9]     any character of: '0' to '9' 
-------------------------------------------------------------------------------- 
    )      end of look-ahead 
-------------------------------------------------------------------------------- 
)      end of look-ahead 
-------------------------------------------------------------------------------- 
    (?=      look ahead to see if there is: 
-------------------------------------------------------------------------------- 
    (?:      group, but do not capture (between 1 and 
          8 times (matching the most amount 
          possible)): 
-------------------------------------------------------------------------------- 
     [^A-Z]*?     any character except: 'A' to 'Z' (0 or 
           more times (matching the least amount 
           possible)) 
-------------------------------------------------------------------------------- 
     [A-Z]     any character of: 'A' to 'Z' 
-------------------------------------------------------------------------------- 
    ){1,8}     end of grouping 
-------------------------------------------------------------------------------- 
    (?!      look ahead to see if there is not: 
-------------------------------------------------------------------------------- 
     .*?      any character except \n (0 or more 
           times (matching the least amount 
           possible)) 
-------------------------------------------------------------------------------- 
     [A-Z]     any character of: 'A' to 'Z' 
-------------------------------------------------------------------------------- 
    )      end of look-ahead 
-------------------------------------------------------------------------------- 
)      end of look-ahead 
-------------------------------------------------------------------------------- 
    (?=      look ahead to see if there is: 
-------------------------------------------------------------------------------- 
    (?:      group, but do not capture (between 1 and 
          8 times (matching the most amount 
          possible)): 
-------------------------------------------------------------------------------- 
     [^!-\/:[email protected][-`{-   any character except: '!' to '\/', ':' 
     ~]*?      to '@', '[' to '`', '{' to '~' (0 or 
           more times (matching the least amount 
           possible)) 
-------------------------------------------------------------------------------- 
     [!-\/:[email protected][-`{-~]   any character of: '!' to '\/', ':' to 
           '@', '[' to '`', '{' to '~' 
-------------------------------------------------------------------------------- 
    ){1,8}     end of grouping 
-------------------------------------------------------------------------------- 
    (?!      look ahead to see if there is not: 
-------------------------------------------------------------------------------- 
     .*?      any character except \n (0 or more 
           times (matching the least amount 
           possible)) 
-------------------------------------------------------------------------------- 
     [!-\/:[email protected][-`{-~]   any character of: '!' to '\/', ':' to 
           '@', '[' to '`', '{' to '~' 
-------------------------------------------------------------------------------- 
    )      end of look-ahead 
-------------------------------------------------------------------------------- 
)      end of look-ahead 
-------------------------------------------------------------------------------- 
    [a-zA-Z0-9!-\/:[email protected][-  any character of: 'a' to 'z', 'A' to 'Z', 
    `{-~]{16,25}    '0' to '9', '!' to '\/', ':' to '@', '[' 
          to '`', '{' to '~' (between 16 and 25 
          times (matching the most amount possible)) 
-------------------------------------------------------------------------------- 
    $      before an optional \n, and the end of the 
          string 
+0

哇,這是偉大的感謝你! 我正在爲我的Laravel框架做一個幫助,用戶kan配置我必須使用哪種密碼/驗證...... 字符串的長度,0-9a-zA-Z&符號等 而這個正則表達式很棒 – mangas

0

說明

在你表達一下aheads像(?=.*[A-Z]{1,})正在測試,看看你有1個或多個A-Z 。如果您有任何數字(大於零)的所需字符類,這些將返回true。讓它繼續測試,看看是否有多個匹配只是多餘的。

^(?=(?:[^a-z]*?[a-z]){1})(?=(?:[^0-9]*?[0-9]){1})(?=(?:[^A-Z]*?[A-Z]){1})(?=(?:[^!-\/:[email protected][- { - 〜] *?[ - /:! - @ - {-~]){1})[a-zA-Z0-9!-\/:[email protected][- { - 〜] {} 16,25 $`

Regular expression visualization

**要看到圖像更好,只需右鍵單擊圖像,並在新窗口中選擇視圖

這個正則表達式將執行以下操作:

  • 要求的字符串是16-25長度
  • 字符串
  • 串中需要至少1 A-Z字符中的任何地方需要至少1 a-z字符的任何地方
  • 至少需要1 0-9字符的任何地方字符串中
  • 需要至少該串中的1個符號的任意位置
  • 允許零個空間

實施例0

現場演示

https://regex101.com/r/oS4mY2/3

示例文本

注意最後一行不匹配,因爲它沒有一個符號

aWoeed1#fde39393aii 
aWoeed1#fde39393AII 
aaaaaaaaaaaaaaaaa1A 

樣品匹配

MATCH 1 
0. [0-19] `aWoeed1#fde39393aii` 

MATCH 2 
0. [20-39] `aWoeed1#fde39393AII` 

說明

NODE      EXPLANATION 
-------------------------------------------------------------------------------- 
^      the beginning of the string 
-------------------------------------------------------------------------------- 
    (?=      look ahead to see if there is: 
-------------------------------------------------------------------------------- 
    (?:      group, but do not capture (1 times): 
-------------------------------------------------------------------------------- 
     [^a-z]*?     any character except: 'a' to 'z' (0 or 
           more times (matching the least amount 
           possible)) 
-------------------------------------------------------------------------------- 
     [a-z]     any character of: 'a' to 'z' 
-------------------------------------------------------------------------------- 
    ){1}      end of grouping 
-------------------------------------------------------------------------------- 
)      end of look-ahead 
-------------------------------------------------------------------------------- 
    (?=      look ahead to see if there is: 
-------------------------------------------------------------------------------- 
    (?:      group, but do not capture (1 times): 
-------------------------------------------------------------------------------- 
     [^0-9]*?     any character except: '0' to '9' (0 or 
           more times (matching the least amount 
           possible)) 
-------------------------------------------------------------------------------- 
     [0-9]     any character of: '0' to '9' 
-------------------------------------------------------------------------------- 
    ){1}      end of grouping 
-------------------------------------------------------------------------------- 
)      end of look-ahead 
-------------------------------------------------------------------------------- 
    (?=      look ahead to see if there is: 
-------------------------------------------------------------------------------- 
    (?:      group, but do not capture (1 times): 
-------------------------------------------------------------------------------- 
     [^A-Z]*?     any character except: 'A' to 'Z' (0 or 
           more times (matching the least amount 
           possible)) 
-------------------------------------------------------------------------------- 
     [A-Z]     any character of: 'A' to 'Z' 
-------------------------------------------------------------------------------- 
    ){1}      end of grouping 
-------------------------------------------------------------------------------- 
)      end of look-ahead 
-------------------------------------------------------------------------------- 
    (?=      look ahead to see if there is: 
-------------------------------------------------------------------------------- 
    (?:      group, but do not capture (1 times): 
-------------------------------------------------------------------------------- 
     [^!-\/:[email protected][-`{-   any character except: '!' to '\/', ':' 
     ~]*?      to '@', '[' to '`', '{' to '~' (0 or 
           more times (matching the least amount 
           possible)) 
-------------------------------------------------------------------------------- 
     [!-\/:[email protected][-`{-~]   any character of: '!' to '\/', ':' to 
           '@', '[' to '`', '{' to '~' 
-------------------------------------------------------------------------------- 
    ){1}      end of grouping 
-------------------------------------------------------------------------------- 
)      end of look-ahead 
-------------------------------------------------------------------------------- 
    [a-zA-Z0-9!-\/:[email protected][-  any character of: 'a' to 'z', 'A' to 'Z', 
    `{-~]{16,25}    '0' to '9', '!' to '\/', ':' to '@', '[' 
          to '`', '{' to '~' (between 16 and 25 
          times (matching the most amount possible)) 
-------------------------------------------------------------------------------- 
    $      before an optional \n, and the end of the 
          string 
相關問題