Omar Gonzales
Omar Gonzales

Reputation: 4008

Regex - str_extract_all returns list and not vector

i need to extract the prices from this vector, without the symbol: "S/."

Eg: "\r\n\t\t\t\t\tS/. 499.95" -> 499.95

Or: "\r\n\t\t\t\t\tS/. 1,099.95" -> 1,099.95

This is my attemp:

precios <- str_extract_all(tvs_prices, "[0-9]*\\,[0-9]*\\.?[0-9]*$")

This is my vector:

 [1] "\r\n\t\t\t\t\tS/. 499.95"   "\r\n\t\t\t\t\tS/. 9,999" "\r\n\t\t\t\t\tS/. 899"      "\r\n\t\t\t\t\tS/. 1,349"
 [5] "\r\n\t\t\t\t\tS/. 1,499" "\r\n\t\t\t\t\tS/. 1,999" "\r\n\t\t\t\t\tS/. 699"   "\r\n\t\t\t\t\tS/. 499"  
 [9] "\r\n\t\t\t\t\tS/. 899"   "\r\n\t\t\t\t\tS/. 999"   "\r\n\t\t\t\t\tS/. 1,099"     "\r\n\t\t\t\t\tS/. 1,299"
 [13] "\r\n\t\t\t\t\tS/. 299"   "\r\n\t\t\t\t\tS/. 699"   "\r\n\t\t\t\t\tS/. 1,099" "\r\n\t\t\t\t\tS/. 1,899"
 [17] "\r\n\t\t\tS/. 1,499"

But i get this results:

a) A list,

b) Some contains "character(0)" within elements of the list.

I don't need a list, but a vector, as a number (as.numeric()):

[[1]]
character(0)

[[2]]
[1] "9,999"

When trying to be more specific, using "^", i just get a list containing: "character(0)":

Code:

precios <- str_extract_all(tvs_prices, "^[0-9]*\\,[0-9]*\\.?[0-9]*$")

For all the elements in the list (i don't need a list but a vector):

[[1]]
character(0)

[[2]]
character(0)

And so on for all the elements on the original vector...

Upvotes: 0

Views: 911

Answers (1)

Andie2302
Andie2302

Reputation: 4897

This regex extracts a number:

\d*(?:,\d{3})*(?:\.\d{2})?

Upvotes: 0

Related Questions