2

I´m trying this

string  <- ":  FC Relacionado con Paciente  FC Protocolo1  FC Comunicacion entre Profesionales1  FC Disponibilidad"


str_extract_all(string, "FC.*1")

It gives back this result:

[1] "FC Relacionado con Paciente  FC Protocolo1  FC Comunicacion entre Profesionales1"

What I want is this:

[1] FC Protocolo1  FC Comunicacion entre Profesionales1"

What should I change?

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81

3 Answers3

4

You can use

library(stringr)
string  <- ":  FC Relacionado con Paciente  FC Protocolo1  FC Comunicacion entre Profesionales1  FC Disponibilidad"
str_extract_all(string, "\\bFC\\b(?:(?!\\bFC\\b).)*?1")

See the R demo. Output:

[[1]]
[1] "FC Protocolo1"                       
[2] "FC Comunicacion entre Profesionales1"

See the regex demo. Note: if there must be no digit/letter/underscore after 1, add another \b there.

Pattern details

  • \bFC\b - a whole word FC
  • (?:(?!\bFC\b).)*? - any single char other than line break chars (if you need to match across line breaks, add (?s) at the start of the pattern), zero or more but as few as possible occurrences, that does not start a whole word FC char sequence
  • 1 - a 1 char.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
2

You can use str_split and str_detect as follows

s <- str_split(gsub('(FC)', '_\\1', string), '_')[[1]]

s[str_detect(s, 'FC.*1')]

Output

[1] "FC Protocolo1  "                       
[2] "FC Comunicacion entre Profesionales1  "

If the string is in a variable of a tibble with name string you can use

s <- str_split(gsub('(FC)', '_\\1', string), '_')

new_column <- sapply(s, \(x) paste0(x[str_detect(x, 'FC.*1')], collapse = ''))
Mohamed Desouky
  • 4,340
  • 2
  • 4
  • 19
1

Another option might be grep + strsplit (but I would say my solution is not as advanced or efficient as @Wiktor's)

> grep("FC.*1", strsplit(string, "\\s+(?=FC)", perl = TRUE)[[1]], value = TRUE)
[1] "FC Protocolo1"
[2] "FC Comunicacion entre Profesionales1"
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81