Regular expression for letters, numbers, and - _

I am having trouble checking in PHP if the value is any of the following combinations

  • letters (upper or lower case)
  • numbers (0-9)
  • underline (_)
  • dash (-)
  • point (.)
  • Without spaces! or other characters

a few examples:

  • OK: "screen123.css"
  • OK: "screen-new-file.css"
  • OK: "screen_new.js"
  • NOT OK: "screen new file.css"

I assume that for this I need a regular expression, since I need to throw an error when the index line contains other characters in it than those mentioned above.

+46
regex
Jun 12 '10 at 12:23
source share
5 answers

The sample you want is similar to ( see it on rubular.com ):

^[a-zA-Z0-9_.-]*$ 

Explanation:

  • ^ is the beginning of the string binding
  • $ is the end of the string binding
  • [...] is a character class definition
  • * - repetition of "zero or more"

Please note that the literal dash is the last character in the definition of the character class, otherwise it has a different meaning (i.e. range). . also has different meanings outside the definition of character classes, but inside it is just literal .

References




In php

Here is a snippet to show how you can use this template:

 <?php $arr = array( 'screen123.css', 'screen-new-file.css', 'screen_new.js', 'screen new file.css' ); foreach ($arr as $s) { if (preg_match('/^[\w.-]*$/', $s)) { print "$s is a match\n"; } else { print "$s is NO match!!!\n"; }; } ?> 

The above prints ( as seen on ideone.com ):

 screen123.css is a match screen-new-file.css is a match screen_new.js is a match screen new file.css is NO match!!! 

Note that the pattern is slightly different using \w . This is the character class for the "word character".

API Links




Specification Note

This seems to fit your specification, but note that it will fit things like ..... etc. that may or may not be what you want. If you can be more specific which pattern you want to match, the regex will be a little more complicated.

The above regex also matches an empty string. If you need at least one character, use + (one or more) instead of * (zero or more) to repeat.

In any case, you can clarify your specification (it always helps when asking a question about regular expression), but I hope you can also learn how to write a template yourself, given the above information.

+86
Jun 12 2018-10-12
source share

To really cover your template, that is, the correct file names according to your rules, I think you need a little more. Please note that this does not match legal file names from a system point of view. It will be system dependent and more liberal in what it accepts. This is designed to fit your acceptable patterns.

 ^([a-zA-Z0-9]+[_-])*[a-zA-Z0-9]+\.[a-zA-Z0-9]+$ 

Explanation:

  • ^ Matches the beginning of a line. This (plus the final match) makes the string match the exact expression, and not just contain the substring that matches the expression.
  • ([a-zA-Z0-9]+[_-])* Zero or more cases of one or more letters or numbers, followed by an underscore or dash. This causes all names containing a dash or underscore to have letters or numbers between them.
  • [a-zA-Z0-9]+ One or more letters or numbers. This covers all names that do not contain underscores or dashes.
  • \. Liter period (period). Forces the file name to have an extension and, excluding from the rest of the template, allow the use of the period between the name and extension. If you need more than one extension that could be processed using the same method as for dashes / underscores, just at the end.
  • [a-zA-Z0-9]+ One or more letters or numbers. The extension must be at least one character long and contain only letters and numbers. This is typical, but if you want to enable underlining, this can also be resolved. You can also specify a range of length {2,3} instead of one or more matches + , if that was more appropriate.
  • $ Match the end of the line. See the start character.
+5
Jun 12 2018-10-12
source share

you can use

 ^[\w\d_.-]+$ 

the + must make sure that it has at least 1 character. ^ And $ needed to indicate the beginning and end, otherwise, if the line has a match in the middle, for example @@@@xyz%%%% , then it still matches.

+4
Jun 12 2018-10-12
source share

Something like this should work

 $code = "screen new file.css"; if (!preg_match("/^[-_a-zA-Z0-9.]+$/", $code)) { echo "not valid"; } 

It will sound "invalid"

+4
Jun 12 '10 at 12:37
source share

[A-Za-z0-9_.-]*

This will also match empty lines if you do not want to exchange the last * for +

+1
Jun 12 2018-10-12
source share



All Articles