Is there an R function to capture a lot of patterns in a text?

88 Views Asked by At

I have the following text in my dataset:

[1] "q negociação c/v tipo mercado prazo especificação do título obs (*) quantidade preço / ajuste valor operação / ajuste d/c 1-bovespa c fracionario magaz luiza on eb nm # 1 25,76 25,76 d 1-bovespa c fracionario magaz luiza on eb nm # 9 25,76 231,84 d 1-bovespa c fracionario magaz luiza on eb nm 40 25,76 1030,40 d 1-bovespa c fracionario mrv on ed nm 40 18,14 725,60 d resumo dos negócios"

I would like to extract the various texts between two standards, specifically the texts contained between "1-bovespa" and "d". Currently, I use the str_extract the readtext package but it does so for only the first pattern found. However, I would like the command to scroll through all the text, and as it finds the pattern again, build a data frame.

I'm trying something like this:

str_extract_all(out, "\\(1-bovespa).+?\\d")
2

There are 2 best solutions below

2
On

here's a different approach using the repeated pattern as delimiters. It's a bit hacky, but seems to work:

library(tidyverse)
text <- "q negociação c/v tipo mercado prazo especificação do título obs (*) quantidade preço / ajuste valor operação / ajuste d/c 1-bovespa c fracionario magaz luiza on eb nm # 1 25,76 25,76 d 1-bovespa c fracionario magaz luiza on eb nm # 9 25,76 231,84 d 1-bovespa c fracionario magaz luiza on eb nm 40 25,76 1030,40 d 1-bovespa c fracionario mrv on ed nm 40 18,14 725,60 d resumo dos negócios"


delim1 <- "1-bovespa "
delim2 <- " d"

result <- strsplit(text, delim1) %>% 
  unlist() %>%
  paste0(delim1, .) %>% 
  strsplit(., " d") %>% 
  unlist() %>% 
  enframe(value = "text", name = NULL) %>% 
  slice(2:nrow(.)) %>%
  mutate(text = paste0(text, delim2)) %>% 
  filter(grepl(delim1, text))

With the result:

result
# A tibble: 4 x 1
  text                                                           
  <chr>                                                          
1 1-bovespa c fracionario magaz luiza on eb nm # 1 25,76 25,76 d 
2 1-bovespa c fracionario magaz luiza on eb nm # 9 25,76 231,84 d
3 1-bovespa c fracionario magaz luiza on eb nm 40 25,76 1030,40 d
4 1-bovespa c fracionario mrv on ed nm 40 18,14 725,60 d  
2
On

Your pattern has parentheses in it - escaped so they are taken literally. Your text does not have parentheses. Also, \d is a special regex to match digits, you want a literal d. I removed the parentheses and the \\, and it seems to work:

out = "q negociação c/v tipo mercado prazo especificação do título obs (*) quantidade preço / ajuste valor operação / ajuste d/c 1-bovespa c fracionario magaz luiza on eb nm # 1 25,76 25,76 d 1-bovespa c fracionario magaz luiza on eb nm # 9 25,76 231,84 d 1-bovespa c fracionario magaz luiza on eb nm 40 25,76 1030,40 d 1-bovespa c fracionario mrv on ed nm 40 18,14 725,60 d resumo dos negócios"
str_extract_all(out, "1-bovespa.+?d")
# [[1]]
# [1] "1-bovespa c fracionario magaz luiza on eb nm # 1 25,76 25,76 d" 
# [2] "1-bovespa c fracionario magaz luiza on eb nm # 9 25,76 231,84 d"
# [3] "1-bovespa c fracionario magaz luiza on eb nm 40 25,76 1030,40 d"
# [4] "1-bovespa c fracionario mrv on ed"