Regex for this format xxxxxxxxxxxx_v2

5

How can I make a regex that accepts in this format:

1q2e5g6s4t5s2u1i5dy1s9u14i2s5u9o_v2

1q2e5g6s4t5s2u1i5dy1s9u14i2s5u9o has 32 characters and then has _v2 - the last 3 are always _v2 .

Examples:

  • xxxxxxxxx_v2 - right
  • a xxxxxxx_v2 - wrong
  • sssssssss - wrong
  • ccccccc_v1 - wrong
  • xxxxxxx_ - wrong
  • kdkd ddd_v2 - wrong

Any ideas?

    
asked by anonymous 27.10.2018 / 22:38

3 answers

10

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$
27.10.2018 / 23:10
5

Try the following expression ( demo regex ):

'/\w{32}_v2/'
    
27.10.2018 / 23:01
5

32 alphanumeric characters are represented by \w{32} , _v2 is represented simply by _v2 , and to ensure that the sequence has to start and end with the rules we define (and not only have that sequence in the middle), we use ^ to signal the beginning, and $ to signal the end, ie:

/^\w{32}_v2$/
    
27.10.2018 / 23:02