moodymudskipper / unglue

Extract matched substrings using a pattern, similar to what package glue does in reverse
GNU General Public License v3.0
159 stars 2 forks source link

catch nested `"{"` so we can use `{` in patterns #2

Closed moodymudskipper closed 5 years ago

moodymudskipper commented 5 years ago

This is the way to do it, adapted from :

https://stackoverflow.com/a/41749376/2270475

s = c("{x} and {y=\\d{2}.*}", "{i} and {j=\\D{2}\\d{3}}")
matched <- gregexpr("\\{(?>[^{}]|(?R))*\\}", s, perl = T)
mapply(function(x,y) substring(x, y, y + attr(y, "match.length") - 1),s,matched)
#>      {x} and {y=\\d{2}.*} {i} and {j=\\D{2}\\d{3}}
#> [1,] "{x}"                "{i}"                   
#> [2,] "{y=\\d{2}.*}"       "{j=\\D{2}\\d{3}}"

Created on 2019-07-15 by the reprex package (v0.3.0)

This way also we don't need a dependency on stringr::str_match_all()

moodymudskipper commented 5 years ago

done

github-actions[bot] commented 2 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.