Search code examples
rregexstringstrsplit

Use str split but based on position of specials character


Hi can anyone help me on this ??

C <- "NURUL AMANI [ID 26378] [IC 971035186514] SYED SAHARR [ID 61839] [IC 981627015412]"
str_split(C, "\\]")

The result is like this.

[1]"NURUL AMANI [ID 26378" " [IC 971035186514" [3]" SYED SAHARR [ID 61839" " [IC 981627015412"

I want the result to be like this

[1]"NURUL AMANI [ID 26378] [IC 971035186514]" [2]" SYED SAHARR [ID 61839] [IC 981627015412]"

Solution

  • Using base, you can do this:

    strsplit(C,"(?<=\\])(?= \\w)",perl = TRUE)
    

    and get with the space before SYED:

    > strsplit(C,"(?<=\\])(?= \\w)",perl = TRUE)
    [[1]]
    [1] "NURUL AMANI [ID 26378] [IC 971035186514]"  " SYED SAHARR [ID 61839] [IC 981627015412]"
    

    If you don't want to keep this space, you can write:

    > strsplit(C,"(?<=\\]) (?=\\w)",perl = TRUE)
    [[1]]
    [1] "NURUL AMANI [ID 26378] [IC 971035186514]" "SYED SAHARR [ID 61839] [IC 981627015412]"