ECII
ECII

Reputation: 10619

Extract a specific number from a string using pattern in R

I have

test<-"owidhsf 2121 .Record: 1111, Field: kjhdlksd 22 33 455"

I would like to extract 1111

I cannot use substr() because the length of the string is variable. But the required output (1111) will always be within the pattern Record: 1111, Field

Tidyverse solution prefered.

Upvotes: 0

Views: 38

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388807

With the latest stringr (1.5.0), you can extract the capturing group with str_extract -

library(stringr)

str_extract(test, 'Record:\\s+(\\d+)', group = 1)
#[1] "1111"

This internally, calls str_match i.e

str_match(test, 'Record:\\s+(\\d+)')[, 2]
#[1] "1111"

Upvotes: 2

akrun
akrun

Reputation: 886938

If the digits succeeds the Record: , then use str_extract with a regex lookaround

library(stringr)
str_extract(test, "(?<=Record: )\\d+")
[1] "1111"

Or in base R, we can capture the digits

sub(".*\\s\\.Record:\\s+(\\d+),\\s*Field.*", "\\1", test)
[1] "1111"

Upvotes: 3

Related Questions