Exclude "I" and "O" from alpha-numeric id in stringi character set

189 Views Asked by At

I know from Generate unique alphanumeric IDs, that I can use stringi and stri_rand_strings to generate a unique alpha-numeric id. I am trying to figure out an efficient way to do so but only include the numbers 0-9 and all letters but "I" and "O". I can't seem to figure out how to include this in the pattern c( LETTERS[c(1:8,10:14,16:26)],"[0-9]")

stri_rand_strings(25, 6)
2

There are 2 best solutions below

0
manotheshark On BEST ANSWER

Modify the pattern to exclude those letters

stri_rand_strings(25, 6, pattern = "[a-zA-HJ-NP-Z0-9]")

[1] "l3e6eH" "NfcjuP" "vtHxWy" "bs2Zd1" "2UGWoJ" "GhettL" "mvvLqi" "AtBBnd" "ijsDFj" "4CXpn6" "MpyUEh" "HZUyDi" "Fba7Af"
[14] "M3lWdn" "A5Vf8D" "tcC9as" "jTXyK5" "U5gUCy" "rnQN1p" "vEouUF" "c8ZU35" "C91o7m" "vuM7iE" "dl49kM" "opucvl"

To only use capital letters

stri_rand_strings(25, 6, pattern = "[A-HJ-NP-Z0-9]")
0
Wiktor Stribiżew On

A more flexible approach is to use ICU regex engine feature called character class subtraction.

To match any ASCII alphanumeric chars except I and O use a [[:alnum:]&\p{ASCII}-[IO]] regex pattern:

  • [ - start of the bracket expression:
    • [:alnum:] - match any Unicode alphanumeric
    • & - AND
    • \p{ASCII} - the alphanumeric must be from the ASCII set
    • -[IO] - but I and O chars
  • ] - end of the bracket expression.

The final solution will look like

stri_rand_strings(25, 6, pattern = "[[:alnum:]&\\p{ASCII}-[IO]]")

Output I got:

[1] "7hWJdu" "cjvekt" "oPqg0C" "pK1JRi" "lrjB2G" "2Zjp0P" "bR7XcK" "V1i8XG" "hojuMU" "4fHpAP" "vSAHFP" "BTXabM" "RWQjaF" "Ac0VbH" "d4GXh1" "kAXWR5" "gx7rQX" "sRXmmw"
[19] "kXcb9H" "mJPuCL" "yBylmm" "wqCtUJ" "zgefj9" "1v6gYY" "l47wjf"