【问题标题】:Match text strings containing quotation marks which are encoded differently匹配包含不同编码的引号的文本字符串
【发布时间】:2018-08-21 20:57:29
【问题描述】:

我有两个包含相同信息的数据框。第一个包含唯一标识符。我想用户 dplyr::inner_join 按标题匹配。

不幸的是,其中一个数据框包含 {"} 来表示引号,而另一个只是使用单引号

例如,我想匹配下面显示的两个标题。

The {"}Level of Readiness{"} for HCV treatment
The 'Level of Readiness' for HCV treatment

【问题讨论】:

  • 您可以使用gsub("[^[:alnum:] ]", "", str) 删除任何非字母数字字符并在之后加入吗?

标签: r dplyr stringr fuzzyjoin


【解决方案1】:

你可以用gsub把它们变成单引号,但是你需要用单引号把{"}括起来,用双引号把'括起来。请注意,fixed = TRUE'{"}' 视为文字字符串而不是正则表达式:

gsub('{"}', "'", 'The {"}Level of Readiness{"} for HCV treatment', fixed = TRUE)
# [1] "The 'Level of Readiness' for HCV treatment"

【讨论】:

  • 当我在 R 中运行上面的代码时,我得到以下错误: gsub("{\"}", "'", "The {\"}Level of Readiness{\" } 用于 HCV 治疗") : 无效的正则表达式 '{"}',原因 'Invalid contents of {}'
  • @user25494 抱歉,我错过了fixed = TRUE 选项。它现在应该可以工作了。
猜你喜欢
  • 1970-01-01
  • 2017-01-26
  • 2013-04-01
  • 2021-06-02
  • 2018-05-11
  • 1970-01-01
  • 1970-01-01
  • 2022-07-06
  • 2019-06-13
相关资源
最近更新 更多