【问题标题】:Extracting numbers after a word and storing the results as a new line separator "\n"提取单词后的数字并将结果存储为换行符“\n”
【发布时间】:2020-09-21 14:35:58
【问题描述】:

我有一些文本数据,如下所示:

text
1  to $2.00 on an ongoing basis. the ongoing eps guidance excludes both a 68 cent-per-share charge associated with the establishment of the solutia-related reserve and a tax benefit of
2     wheat and barley business. on a reported basis, eps is in the range of $1.56 to $1.71 per share for the full fiscal year. (for a reconciliation of ongoing... eps was 4.56 to 4.98
3                                    the year ago quarter while 2004 full year eps was $.93, up 7.7% from 2003. return on equity was 21.7% for the fourth quarter and 20.4% for the full

我正在尝试从中提取一些信息。我想提取单词eps 之后的第一个数字。我可以做到以下几点:

data %>% 
  mutate(
    firstNumberAfterWord = str_match_all(text, "eps\\D*(\\d+)")
  )

这给出了:

                 firstNumberAfterWord
1 eps guidance excludes both a 68, 68
2        eps is in the range of $1, 1    # This is wrong. It should be "$1.56 to $1.71"
3                    eps was $.93, 93

这不符合我的要求,因为它拉动了 68193,但 1 不正确。我查看了map_chr(myWordColumn, str_c, collapse = "\n"),,首先将其折叠,然后提取单词,但没有运气。

我想提取eps 单词之后的第一个数字(eps 单词的所有出现,其中每个出现由"\n" 分隔符分隔。

预期的输出将是有一个新列,其中包含:

$.93 # since this comes after the part "eps was $.93"
68 # since it comes after "eps guidance excludes both a 68"
$1.56 to $ 1.71 # "eps is in the range of $1.56 to $1.71" # On a new line for this observation
 4.56 to 4.98 # eps was 4.56 to 4.98

这些都在eps 之后。

数据:

    data <- data.frame(
  text = c(" to $2.00 on an ongoing basis. the ongoing eps guidance excludes both a 68 cent-per-share charge associated with the establishment of the solutia-related reserve and a tax benefit of", 
           " wheat and barley business. on a reported basis, eps is in the range of $1.56 to $1.71 per share for the full fiscal year. (for a reconciliation of ongoing... eps was 4.56 to 4.98",
           " the year ago quarter while 2004 full year eps was $.93, up 7.7% from 2003. return on equity was 21.7% for the fourth quarter and 20.4% for the full"
  )
)

【问题讨论】:

    标签: r regex


    【解决方案1】:

    我建议使用以下模式:

    \beps\b\D*?(\p{Sc}?\d*\.?\d+(?:\s*(?:to|[\xAD\p{Pd}])\s*\d*\.?\d+)?)
    

    请参阅regex demo详情

    • \beps\b - 一个完整的词eps
    • \D*? - 0 个或更多非数字字符,尽可能少
    • (\p{Sc}?\d*\.?\d+(?:\s*(?:to|\p{Pd})\s*\d*\.?\d+)?) - 第 1 组:
      • \p{Sc}? - 可选货币符号
      • \d*\.?\d+ - 整数或浮点数
      • (?:\s*(?:to|[\xAD\p{Pd}])\s*\d*\.?\d+)? - 可选出现
        • \s* - 0 个或多个空格
        • (?:to|[\xAD\p{Pd}]) - to 或任何 Unicode 破折号
        • \s* - 0 个或多个空格
        • \d*\.?\d+ - 整数或浮点数

    an R demo:

    library(stringr)
    data <- data.frame(
      text = c(" to $2.00 on an ongoing basis. the ongoing eps guidance excludes both a 68 cent-per-share charge associated with the establishment of the solutia-related reserve and a tax benefit of", 
               " wheat and barley business. on a reported basis, eps is in the range of $1.56 to $1.71 per share for the full fiscal year. (for a reconciliation of ongoing... eps was 4.56 to 4.98",
               " the year ago quarter while 2004 full year eps was $.93, up 7.7% from 2003. return on equity was 21.7% for the fourth quarter and 20.4% for the full"
      )
    )
    res <- str_match_all(data$text, "\\beps\\b\\D*?(\\p{Sc}?\\d*\\.?\\d+(?:\\s*(?:to|[\\xAD\\p{Pd}])\\s*\\d*\\.?\\d+)?)")
    data$result <- lapply(res, function(x) paste(x[,-1], collapse="\n"))
    

    输出:

    text
    1  to $2.00 on an ongoing basis. the ongoing eps guidance excludes both a 68 cent-per-share charge associated with the establishment of the solutia-related reserve and a tax benefit of
    2     wheat and barley business. on a reported basis, eps is in the range of $1.56 to $1.71 per share for the full fiscal year. (for a reconciliation of ongoing... eps was 4.56 to 4.98
    3                                    the year ago quarter while 2004 full year eps was $.93, up 7.7% from 2003. return on equity was 21.7% for the fourth quarter and 20.4% for the full
                   result
    1                  68
    2 $1.56\n4.56 to 4.98
    3                $.93
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      • 2018-06-13
      • 2016-11-03
      • 1970-01-01
      • 2012-08-25
      • 2020-02-22
      相关资源
      最近更新 更多