r中的正则表达式误解

时间:2016-05-26 14:25:50

标签: r gsub stringr stringi

我似乎不理解gsub或stringr。 例如:

{
  "products_all" : {
    "-KFOiZDrbR4eq4fXSdMZ" : {
      "affiliate_url" : "https://some.link.com",
      "description" : "product description",
      "image_url" : "https://image.url.com/firstImage.jpg",
      "images" : [ "https://image.url.com/firstImage.jpg", "https://image.url.com/secondImage.jpg" ],
      "merchant" : "merchant.com",
      "price_usd" : "100.00",
      "title" : "product title"
    },
    ...,
    "-KFtOmT_Hq-48chbHh7I" : {
      "affiliate_url" : "https://another.link.com",
      "description" : "another product description",
      "image_url" : "https://image.link.com/anotherImage.jpg",
      "images" : [ "https://image.link.com/anotherImage.jpg" ],
      "merchant" : "anothermerchant.com",
      "original_currency" : "GBP",
      "original_price" : "255",
      "price_usd" : "364.65",
      "product_category" : "Bracelet",
      "external_product_id" : "Anothermerchant-123",
      "title" : "another product title"
    },
    ...,
    "-KFtOmTlcb5-PTQ6kTv8" : {
      "affiliate_url" : "https://last.product.com",
      "description" : "last product description",
      "image_url" : "https://last.product.com/image.jpg",
      "images" : [ "https://last.product.com/image.jpg" ],
      "merchant" : "anothermerchant.com",
      "original_currency" : "GBP",
      "original_price" : "179",
      "price_usd" : "268.5",
      "product_category" : "Bracelet",
      "scraped_product_id" : "anothermerchant-493",
      "title" : "last product title"
    }
  }
}

好。但是:

 > a<- "a book"

> gsub(" ", ".", a)

[1] "a.book"

我希望

  

“一本书”

我用空格替换了句号。

另外:> a<-"a.book" > gsub(".", " ", a) [1] " " srintr返回:

str_replace(a, ".", " ")

" .book"返回

str_replace_all(a, ".", " ")

我可以使用" "stringi

stri_replace(a, " ", fixed=".")

我只是想知道为什么gsub(和str_replace)没有按照我的预期行事。它们在用另一个角色替换空间时起作用,但不是相反。

1 个答案:

答案 0 :(得分:4)

那是因为gsub的第一个参数,即pattern实际上是一个正则表达式。在正则表达式中,句点.是一个元字符,它匹配任何单个字符,请参阅?base::regex。在您的情况下,您需要以下列方式逃避期间:

gsub("\\.", " ", a)