Regex flags, comments
comments = TRUE
tweaks the pattern language to ignore spaces and new lines, as well as everything after #.- use comments and whitespace to make complex regular expressions more understandable
- using
comments
and want to match a space, newline, or #, you’ll need to escape it with\
.
phone <- regex(
r"(
\(? # optional opening parens
(\d{3}) # area code
[)\-]? # optional closing parens or dash
\ ? # optional space
(\d{3}) # another three numbers
[\ -]? # optional space or dash
(\d{4}) # four more numbers
)",
comments = TRUE
)
str_extract(c("514-791-8141", "(123) 456 7890", "123456"), phone)
## [1] "514-791-8141" "(123) 456 7890" NA