Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match letters in R regex

Tags:

regex

r

Suppose I run the following

txt <- "client:A, field:foo, category:bar"
grep("field:[A-z]+", txt, value = TRUE, perl = TRUE)

Based on regexr.com I expected I would get field:foo, but instead I get the entire string. Why is this?

like image 301
T'n'E Avatar asked Nov 21 '25 01:11

T'n'E


1 Answers

You seem to want to extract the value. Use regmatches:

txt <- "client:A, field:foo, category:bar"
regmatches(txt, regexpr("field:[[:alpha:]]+", txt))
# => [1] "field:foo"

See the R demo.

To match multiple occurrences, replace regexpr with gregexpr.

Or use stringr str_extract_all:

library(stringr)
str_extract_all(text, "field:[a-zA-Z]+")

Another point is that [A-z] matches more than ASCII letters. Use [[:alpha:]] in a TRE (regexpr / gregexpr with no perl=TRUE)/ICU (stringr) regex to match any letter.

like image 91
Wiktor Stribiżew Avatar answered Nov 22 '25 16:11

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!