使用正则表达式允许使用字母字符,超量,下划线,空格和数字

时间:2017-07-27 13:26:07

标签: laravel validation

我想使用Laravel验证一个独特的情况。我授权的领域是一本书的名称。因此它可以包含字母字符,数字字符,空格和超大/下划线/任何其他键。在您输入任何键之前,我唯一不想要的就是空格。所以名称不能是“L”,注意空格,而“L L L”是完全可以接受的。在这种情况下,有人能帮助我吗?

到目前为止,我得到了正则表达式验证:

regex:[a-z{1}[A-Z]{1}[0-9]{1}]

我不确定如何包含其他限制。

2 个答案:

答案 0 :(得分:2)

  • 简答:

对于带空格的alpha_num,请使用此regEx:

C:\Users\bre\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/bre/PycharmProjects/test/TkinterApp/test13.py
Traceback (most recent call last):
  File "C:/Users/bre/PycharmProjects/test/TkinterApp/test13.py", line 1, in <module>
    import win32com.client
  File "C:\Users\bre\AppData\Local\Programs\Python\Python36-32\lib\site-packages\win32com\__init__.py", line 5, in <module>
    import win32api, sys, os
ImportError: DLL load failed: The specified module could not be found.

Process finished with exit code 1
  • 比较长一点:)

以下是一些定义的regEx:

DLL

如果你想添加一些其他字符,你应该做的就是将它添加到'regex:/^[\s\w-]*$/' 块。

例如,如果您想要允许^ ==> The circumflex symbol marks the beginning of a pattern, although in some cases it can be omitted $ ==> Same as with the circumflex symbol, the dollar sign marks the end of a search pattern . ==> The period matches any single character ? ==> It will match the preceding pattern zero or one times + ==> It will match the preceding pattern one or more times * ==> It will match the preceding pattern zero or more times | ==> Boolean OR – ==> Matches a range of elements () ==> Groups a different pattern elements together [] ==> Matches any single character between the square brackets {min, max} ==> It is used to match exact character counts \d ==> Matches any single digit \D ==> Matches any single non digit caharcter \w ==> Matches any alpha numeric character including underscore (_) \W ==> Matches any non alpha numeric character excluding the underscore character \s ==> Matches whitespace character ==&gt; []

PS:还有一件事,如果你想要一个像我们这样的setial char \ *或者。你必须像这样逃避他们\ *。

, ==&gt; 'regex:/^[\s\w-,]*$/'

答案 1 :(得分:0)

检查这种模式:

<?php

$pattern = '/^(?=[^ ])[A-Za-z0-9-_ ]+$/';
$test = ' L';

if (preg_match($pattern, $test)) {
    echo 'matched';
} else {
     echo 'does not match';   
}

?>