Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve digits after specific string in R

Tags:

regex

r

I have a bunch of strings that contain the word "radius" followed by one or two digits. They also contain a lot of other letters, digits, and underscores. For example, one is "inflow100_radius6_distance12". I want a regex that will just return the one or two digits following "radius." If R recognized \K, then I would just use this:

radius\K[0-9]{1,2}

and be done. But R doesn't allow \K, so I ended up with this instead (which selects radius and the following numbers, and then cuts off "radius"):

result <- regmatches(input_string, gregexpr("radius[0-9]{1,2}", input_string))
result <- unlist(substr(result, 7, 8)))

I'm pretty new to regex, so I'm sure there's a better way. Any ideas?

like image 310
seaotternerd Avatar asked Dec 19 '22 06:12

seaotternerd


1 Answers

\K is recognized. You can solve the problem by turning on the perl = TRUE parameter.

result <- regmatches(x, gregexpr('radius\\K\\d+', x, perl=T))
like image 119
hwnd Avatar answered Jan 01 '23 14:01

hwnd