I've been answering this Creating a dataframe with text from a website and I have experienced an odd case which I cannot wrap my head around.
We have the lines below copied to our clipboard:
Leading Men (Average American male: 5 feet 9.5 inches)
Dolph Lundgren — 6 feet 5 inches
John Cleese — 6 feet 5 inches
Leading Ladies (Average American female: 5 feet 4 inches)
Uma Thurman — 6 feet 0 inches
Brooke Shields — 6 feet 0 inches
I have provided the solution below, which extracts gender from the header line and fills the following lines/rows with that. The issue here's that it extracts the word 'Leading' as well as "gender". I was expecting to be able to use \K (reset match token) to get rid of that, but that does not work.
web.lines <- read.delim("clipboard", header = F) # reading data from clipboard
library(tidyverse)
web.lines %>%
mutate(gender = str_extract(V1, "Leading\\s+\\b(\\w+)\\b")) %>%
fill(gender , .direction = "down") %>%
group_by(gender) %>%
slice(-1) %>% # removing the headers
separate(V1, into = c("Name", "Height"), sep = " — ")
#> # A tibble: 4 x 3
#> # Groups: gender [2]
#> Name Height gender
#> <chr> <chr> <chr>
#> 1 Uma Thurman 6 feet 0 inches Leading Ladies
#> 2 Brooke Shields 6 feet 0 inches Leading Ladies
#> 3 Dolph Lundgren 6 feet 5 inches Leading Men
#> 4 John Cleese 6 feet 5 inches Leading Men
What I have tried was Leading\\s+\\K\\w+ which seems to be working in the demo https://regex101.com/r/pYaW7a/1 but not with str_extract.