Home > Software design >  Replacing the first and the last letter of a string
Replacing the first and the last letter of a string

Time:08-13

I found this function that switches the first and the last letters in words.

library(stringr)

words |>
    str_replace("([a-zA-Z])(.*)([a-zA-Z])","\\3\\2\\1") |>
    head(25)
[1] "a"         "ebla"      "tboua"     "ebsoluta"  "tccepa"    "tccouna"  
[7] "echieva"   "scrosa"    "tca"       "ectiva"    "lctuaa"    "dda"      
[13] "sddresa"   "tdmia"     "edvertisa" "tffeca"    "dffora"    "rftea"    
[19] "nfternooa" "ngaia"     "tgainsa"   "ega"       "tgena"     "oga"      
[25] "egrea"

Can somebody explain how it works?

What do these mean: "([a-zA-Z])(.*)([a-zA-Z])" and "\\3\\2\\1")?

CodePudding user response:

These are regular expressions (regex) and are used for complex operations on character strings.

str_replace() identifies, from a given string, patterns listed in the first string, and replaces them with the pattern in the second string.

([a-zA-Z])(.*)([a-zA-Z]) means:

  1. Identify three groups (delimited by brackets: ()()()
  2. The first and third group contain a single letter from a to z (or A to Z): [a-zA-Z]
  3. The third group contains any number (*) of any characters (.)

Together, this means: identify the first and last letter of each string.

For more info I recommend the R4DS chapter on strings.

You can also paste a regex on regex101 to get an explanation - but it's a bit technical if you don't know the basics.

  • Related