The other answers suggested using \w
, which actually accepts alphanumeric characters (letters or numbers), but also accepts the _
character.
This means that ^\w{32}_v2$
also considers valid a string that has 32 characters _
before _v2
(that is, the _________________________________v2
string would be considered valid). See here this regex running.
If this is what you want, fine. But if you want to limit to only 32 letters and numbers (and do not accept any other _
before _v2
), change the regex to ^[a-zA-Z0-9]{32}_v2$
. See the here difference.
Brackets ( []
) represent a character class . That means any character inside them does. [ab]
, for example, means "the letter a
or the letter b
" (any one of them works).
In this case, I placed the shortcuts a-z
(any letter from a
to z
), A-Z
(any letter from A
to Z
) and 0-9
digit from 0
to 9
). That is, [a-zA-Z0-9]
will only accept these characters, while \w
also accepts the _
character ( \w
is nothing more than a shortcut to [A-Za-z0-9_]
).
PS : Depending on the language / engine / configuration, \w
may be even more comprehensive. For example, if Unicode is enabled, it can accept Japanese, Arabic, and many other characters. (see examples here and here >).
Generally this option is not enabled by default, but if you want to ensure that only alphabets and digits 0-9 are accepted, use [a-zA-Z0-9]
.
\w
can accept all the cases you need, but also accept others you might not need (strings with _
before _v2
). Again, if this is not a problem, then use \w
. But if this is a problem and you want to avoid these false positives, be as specific as possible and use [a-zA-Z0-9]
.
It was not very clear whether you want "exactly 32 characters" or "1 to 32 characters" before _v2
. Anyway, just change the regex according to what you need:
- exactly 32 characters:
^[a-zA-Z0-9]{32}_v2$
- 1 to 32 characters:
^[a-zA-Z0-9]{1,32}_v2$