jacky_learns_to_code
jacky_learns_to_code

Reputation: 824

Regex to extract patterns from string

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:

  1. Part 1 - a vector of ("J TOTAL 7/0", "Y TOTAL 118/0")
  2. Part 2 - a vector of ("2", "12")

Any help would be much appreciated!

Upvotes: 0

Views: 102

Answers (2)

GKi
GKi

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

akrun
akrun

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

Related Questions