4

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)
smci
  • 32,567
  • 20
  • 113
  • 146
MatthewR
  • 2,660
  • 5
  • 26
  • 37
  • `pattern` argument has to be one character set (between surrounding `[...]`), you can't concatenate multiple sets with `[A-H][0-9]` as you're doing. – smci Dec 12 '19 at 16:45

2 Answers2

8

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]")
manotheshark
  • 4,297
  • 17
  • 30
1

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"
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563