How to resolve spaces in this regex?

I am such an amateur in regex, how do I resolve spaces (no matter how much) in this regex?

if(preg_match('/[^A-Za-z0-9_-]/', $str)) return FALSE; 
+7
php regex
source share
6 answers
 if(preg_match('/[^A-Za-z0-9_ -]/', $str)) return FALSE; 

Notice that I put space in front of the hyphen. If the space were after the hyphen, I would indicate a range of characters from underscore to place. (The problem can also be fixed by setting a backslash before the hyphen to avoid it.)

This assumes that you mean "allow": this regular expression is used to validate a character string, and if it matches, then the character string is not allowed (hence return FALSE ). Thus, characters in a negative character class ( [^...] ) are actually valid characters. (This causes some general confusion on this issue.)

+20
source share

the \s in a regular extension like this '/[^A-Za-z0-9_-\s]/'
means space

+15
source share

There are not many answers to your question, but a site that I find useful for checking regular expression expressions . He also explains that each part of the expression does / means when you hover over it in the input field.

+2
source share

Your question is not clear. The regular expression, as it is, will succeed if $str has any character in it that is not A-Za-z0-9_- . Since the space is not one of these characters, the regular expression will match, and the whole statement returns FALSE .

If this is not what you want and you want your regular expression to match, if $str has any character that is not in A-Za-z0-9_- , or a space, then you need to change it to A-Za-z0-9_ - (note the space between the underscore and hyphen). That way, when your string has a character that is not A-Za-z0-9_ - , the regular expression will match, and your statement will return FALSE . If your line consists of all A-Za-z0-9_ - , then the regular expression will not match, and your processing will continue to the next line.

Edit: Here is an example: if your string is abc123def , at present the regular expression will not match and you will not return FALSE . If your string is abc123 def , the regex will match and the statement will return FALSE . If you change the character class to A-Za-z0-9_ - , the regular expression will not match both abc123def and abc123 def , and you will not return FALSE .

+2
source share

If you need to ALLOW only space, you will need '//'

If you need to ALLOW any char spaces (space, tab, new line) - use '/ \ s /'

And if you need to add a space to your template (means to ignore space) - use / [^ A-Za-z0-9 _ \ -] /

+1
source share

You just need to add a literal space in your class of negative characters.

It would be more concise to use:

  • case-insensitive flag i and skip one of the letter ranges
  • and write [0-9] as \d

like this:

 if (preg_match('/[^A-Z0-9_ -]/i', $str)) return FALSE; 

but it would be even better to use \w which represents [A-Za-z0-9_] (which is most of your pattern) to write:

 if (preg_match('/[^\w -]/', $str)) return FALSE; 

This template states that if $str contains any character that is not in [A-Za-z0-9_ -] , false will be returned.

0
source share

All Articles