Reputation: 824
I have this string
info <- "/J TOTAL 7/0 2|/Y TOTAL 118/0 12"
How can I apply str_extract_all()
from the stringr
package with the correct regular expression to extract them into:
("J TOTAL 7/0", "Y TOTAL 118/0")
("2", "12")
Any help would be much appreciated!
Upvotes: 0
Views: 102
Reputation: 39667
You can try strsplit
and strcapture
instead of str_extract_all()
x <- strsplit(info, "|", TRUE)[[1]]
strcapture("^/(.*?) +(\\d+)$", x, data.frame(a=character(), b=character()))
# a b
#1 J TOTAL 7/0 2
#2 Y TOTAL 118/0 12
Upvotes: 3
Reputation: 887118
Another option with creating a delimiter i.e. replace more than 2 adjacent whitespace character with ,
using gsub
and then read with read.csv
from base R
read.csv(text = chartr("|", "\n", gsub("\\s{2,}", ",", info)), header = FALSE)
V1 V2
1 /J TOTAL 7/0 2
2 /Y TOTAL 118/0 12
Upvotes: 2