[R] regex - extracting 2 numbers and " from strings

David Winsemius dwinsemius at comcast.net
Fri Oct 9 01:14:10 CEST 2015


On Oct 8, 2015, at 3:45 PM, Omar André Gonzáles Díaz wrote:

> Hi I have a vector of 100 elementos like this ones:
> 
> a <- c("SMART TV LCD FHD 70\" LC70LE660", "LED FULL HD 58'' LE58D3140")
> 
> I want to put just the (70\") and (58'') in a vector b.

> sub("(^.+ )(\\d+)([\"]|[']{2})(.+$)", "\\2\\3", a)
[1] "70\"" "58''"

Also. The `stringr` package uses the code in the `stringi` package to give more compact expressions. You might want to look at 

str_extract	Extract matching patterns from a string.
str_extract_all	Extract matching patterns from a string.


> 
> This is my try, but is not working:
> 
> b <- grepl('^[0-9]{2}""$',a)
> 
> Any hint is welcome, thanks.
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list