Home > Net >  Extract a specific number from a string using pattern in R
Extract a specific number from a string using pattern in R

Time:12-08

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.

CodePudding user response:

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"

CodePudding user response:

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"
  • Related