StringR : extract number from string

Hi All,

I am not getting proper output from string .

ex: but couldn't able to get alphanumeric from string.

I need to get FCP1 value from below string ,please advise

x= Book FCP1 is missing in BOOKS setup. Book may be new .
am using below code:

sub(".^ Book\|is * ", " ", x)

Is it always FCP*, * being a numeric value ? Can you find a regex for what you want to extract ?

Keeping on the approach you started with, by replacing before and after by "" you can do

library(stringr)
#> Warning: le package 'stringr' a été compilé avec la version R 3.5.2
x="Book FCP1 is missing in BOOKS setup. Book may be new."
str_replace_all(x, "^Book | is .*", "")
#> [1] "FCP1"

gsub("^Book | is .*", "", x)
#> [1] "FCP1"

Created on 2019-03-10 by the reprex package (v0.2.1)

Another option is to use extract instead of replace, it's hard to know if this approach is going to generalize well with the rest of your data since you are just providing one text string as sample.

library(stringr)
x="Book FCP1 is missing in BOOKS setup. Book may be new."
str_extract(x, "(?<=Book\\s)[A-Z]+\\d")
#> [1] "FCP1"
2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.