r - Isolating specific numeric values in text -
i have significant amount of text trawl through on text csv file of various properties find square metre numeric value of properties is. example:
string <- "this wonderful 120 sqm flat stunning view"
i know can use following extract numeric value:
sqm <- as.numeric(gsub("\\d", "", string))
which returns numeric vector of '120', should. however, wondering if there more sophisticated way accomplish this, given there other irrelevant numeric values in text?
is there way search 'sqm' , return numbers precede it? many comments.
i believe regex lookahead should work:
library(stringr) ## string <- "this wonderful 120 sqm flat stunning view" re <- "((\\d+)(?=\\s?sqm))" ## r> str_extract(string, perl(re)) [1] "120"
Comments
Post a Comment