3

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]"
Rajith Thennakoon
  • 3,975
  • 2
  • 14
  • 24
Wawa
  • 73
  • 9

3 Answers3

1

looks like a regex may be a better fitting for your problem

str_extract_all(C, "\\w+ \\w+ \\[ID [0-9]+\\] \\[IC [0-9]+\\]")
Edo
  • 7,567
  • 2
  • 9
  • 19
1

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]"
dc37
  • 15,840
  • 4
  • 15
  • 32
  • FYI: It is safer to use `perl = TRUE` as `T` might have been redefined earlier in the code. – Wiktor Stribiżew Nov 27 '19 at 07:54
  • @WiktorStribiżew, you're right ;) . It's just that I never use T or F as variable in my code in order to avoid this kind of conflict – dc37 Nov 27 '19 at 07:55
0
stringr::str_split(C, "(?<=\\d\\])(?= \\w)")

Use look behind (https://www.regular-expressions.info/lookaround.html) to find the boundry.

EDIT: fixed as per @dc37's comments

crlwbm
  • 502
  • 2
  • 10