Regex allows only 1 character

后端 未结 6 1439
长情又很酷
长情又很酷 2021-01-22 03:50
$rex = \'/^[^<,\"@?=>|;#]$/i\';

I\'m having trouble with this regular expression. The idea is that the input fields are checked for certain chara

相关标签:
6条回答
  • 2021-01-22 03:56

    ^ means beginning of line
    $ means end of line
    [] means match one out of a group of character

    You will match lines containing one and only one among that list.

    0 讨论(0)
  • 2021-01-22 04:03

    You have the $ after your expression and the ^ in the beginning, which means you are accepting exactly one character.

    EDIT (based on the comments):

    You can try to see if your input fields only have valid characters, by matching it against this (if it matches, it means there are no invalid characters):

    $rex = '/^[^<,"@$?=>|;#]+$/i'
    

    You can also do the reverse, that is, test if your input fields have any invalid characters, using the regex provided by chaos:

    $rex = '/[<,"@$?=>|;#]/';
    

    This way, if the regex matches, then it means you have invalid characters.

    0 讨论(0)
  • 2021-01-22 04:04

    What you probably actually need is:

    $rex = '/[<,"@$?=>|;#]/';
    

    Then your error case is when this regex matches, not when it doesn't.

    This is equivalent to doing what you're currently doing with this small change to your regex:

    $rex = '/^[^<,"@?=>|;#]*$/i';
    

    This is kind of nonsensically overcomplex, though. Like trying to find out whether there's an elephant in the room by counting how many things in the room aren't elephants, then seeing if that number is the same as the number of things in the room. (And the /i modifier is not, at any time, accomplishing anything.)

    0 讨论(0)
  • 2021-01-22 04:08

    maybe "/(.*?)[^<,"@$?=>|;#]/i" or "/^[^<,"@$?=>|;#].*+$/i"

    don't know exactly what you are trying to do

    0 讨论(0)
  • 2021-01-22 04:09

    You'll want '/^[^<,"@$?=>|;#]+$/i' or '/^[^<,"@$?=>|;#]*$/i'.

    For an example:

    $valid = 'Hello';
    $invalid = 'h<i';
    $regex = '/^[^<,"@$?=>|;#]+$/i';
    
    print "'$valid' gives ".preg_match($regex, $valid)."\n";
    print "'$invalid' gives ".preg_match($regex, $invalid)."\n";
    

    Which outputs:

    'Hello' gives 1
    'h<i' gives 0
    

    What I simply did was take your expression and add a + or * after your character group.

    In regular expressions, * means match 0 or more occurences, and + means match 1 or more.

    Since ^ means the beginning of the string and $ the end, without a + or *, you tell it to match a string the consists of exactly one occurrence of a non-special character, thus why it errors if your string is longer than one character.

    If you wish, you can also remove the i at the end of the expression, as you don't need to do a case-insensitive match when no letters are involved in your expression.

    For more information on regular expressions, take a look at Regular-Expressions.info.

    0 讨论(0)
  • 2021-01-22 04:15

    You're asking for only one character. If you want multiple characters, get the pattern repeated, either like this (one or more times):

     $rex = '/^[^<,"@?=>|;#]+$/i';
    

    or like this (zero or more times):

     $rex = '/^[^<,"@?=>|;#]*$/i';
    
    0 讨论(0)
提交回复
热议问题