Search code examples
regexpreg-match

Regexp to accept ONLY Yes or No and should not be case sensitive


Here is my regexp to accept only Yes or No (not case sensitive). Is there a better way to do it? Thanks

preg_match('/^(?:Yes\b|YES\b|yes\b|No\b|NO\b|no\b)/',$input)

Solution

  • You may use i modifier.

    preg_match('~^(?:Yes|no)\b~i', $input);
    

    Add end of the line anchor $ if necessary.

    preg_match('~^(?:Yes|no)$~i', $input);