0

I have the following value (and similar formatting in hundreds of thousands of fields):

61.00.62.1

that I would like to use a stringr or stringi and (likely) a regex to turn into

61.0
0.6
2.1

I have been unsuccessfully using the stringi::stri_split_regex command of the form

"[0-9]\\d{1,2}(\\.[0-9]\\d{1})"

Thank you.

BenD
  • 21
  • 2
  • 2
    You need to add some more detail. Will it always be just 2 digits between periods? If not, what about, for example, `61.1.62` or `61.025.6`? Where would you split those? – elixenide Sep 27 '17 at 05:21
  • 1
    With your example (`x<-"61.00.62.1"`), something like `strsplit(x,"(?<=\\.[0-9])",perl=TRUE)` works. However, as @EdCottrell pointed out, you should define better how the split are obtained. – nicola Sep 27 '17 at 05:25

3 Answers3

0

I would try stri_extract_all_regex:

stringi::stri_extract_all_regex('61.00.62.1', '\\d{1,2}\\.\\d')
# [[1]]
# [1] "61.0" "0.6"  "2.1" 

\\d{1,2}\\.\\d will match 1 to 2 digits followed by one point and one digit.

mt1022
  • 16,834
  • 5
  • 48
  • 71
0

We can use strsplit

strsplit(str1, "(?<=\\..)", perl = TRUE)[[1]]
#[1] "61.0" "0.6"  "2.1" 

data

str1 <- "61.00.62.1"
akrun
  • 874,273
  • 37
  • 540
  • 662
-2

Make list of given string and separate list by each character by checking '.'

Nitin Ware
  • 109
  • 9