return which pattern matched

Hello R experts,

I have two strings like:
A1='stringishere'
A2='stre|st.*re'
I hope to make it to return 'st.*re' because A1 matches the second pattern in A2. Your suggestion will be appreciated. Thanks.

Best,
Veda

If the code below does not do what you want, please provide more details preferably in the form of a Reproducible Example.

library(stringr)
library(purrr)
#> Warning: package 'purrr' was built under R version 3.5.3
A1='stringishere'
A2='stre|st.*re'

Pieces <- str_split(A2, "\\|")[[1]]
Pieces[map_lgl(Pieces, grepl, A1)]
#> [1] "st.*re"

Created on 2019-06-02 by the reprex package (v0.2.1)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.