From 5 characters, you can use * -? and numbers. How to register it? [A-Za-z0-9] {5,} how else * -? to add?

  • put hyphen / minus at the beginning, then it will not go as a range, for example [-a-z0-9], example regex101.com/r/rAbwc5/1 - Jean-Claude

2 answers 2

When specifying character classes, you can do without escaping special characters.

So directly and write an asterisk, a question mark and a dash inside the square brackets. That's just a minus to be placed with the edge, next to the square bracket. Otherwise, it will be perceived as a range of characters. Like that:

[A-Za-z0-9*?-]{5,} 

To comment: To put a forward slash ( / ) in the list of allowed characters, it is advisable to change the delimiters (the so-called characters on the edges of the expression, which are interpreted by PHP functions). Here is an example:

 preg_match('~[A-Za-z0-9*?/-]{5,}~', $str) 
  • the letters cannot be .... - Jean-Claude
  • how else to add there? I can't back it up - Andrii
  • @Andrii, this is probably due to the fact that you use straight slashes as separators. To have a slash in the regular expression, you can either change the separators or use the backslash. - mymedia
  • All figured out. \ / - Andrii
  • @Andrii, supplemented the answer - mymedia

Try this

 ^[\*-\?\d]{3,5}$