Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

regex substitution "." to "_"

I have a specific issue with character substitution in strings:

If I have the following strings

"..A.B....c...A..D.."
"A..S.E.Q.......AW.."
".B.C..a...R......Ds"

Which regex substitution should I use to replace the dots and obtain the following strings:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"A_B_c_A_D"
"A_S_E_Q_AW"
"B_C_a_R_Ds"

I am using R.

Thanks in advance!

>Solution :

Using stringr from the ever fantastic tidyverse.

str1 <- "..A.B….c…A..D.."

str1 %>%
  #replace all dots that follow any word character ('\\.' escapes search, '+' matches one or more, '(?<=\\w)' followed by logic)
  str_replace_all('(?<=\\w)\\.+(?=\\w)', '_') %>%
  #delete remaining dots (i.e. at the start)
  str_remove_all('\\.')

As always plenty of ways to skin the cat with regex

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading