【问题标题】:Improve my Regex to include numbers that contain decimals and percentage signs改进我的正则表达式以包含包含小数和百分号的数字
【发布时间】:2018-09-21 05:14:24
【问题描述】:

我有以下正则表达式,它将捕获前 N 个单词并在下一个句号、感叹号或问号处结束。我需要获取单词数量不同但我想要完整句子的文本块。

regex = (?:\w+[.?!]?\s+){10}(?:\w+,?\s+)*?\w+[.?!]

它适用于以下文本:

仅从虾壳中提取稻草和壳聚糖 占 2、4、6、8 和 10% 发现提取物秸秆 8% 是 对抑制藻类 Microcystis spp 的生长非常有效。 细胞数量和叶绿素 a 的量在过程中减少 治疗。两个值都连续下降,直到试验结束。

https://regex101.com/r/ardIQ7/5

但它不适用于以下文本:

仅从虾壳中提取的稻草和壳聚糖就占了 对于 2、4、6、8 和 10% 的人发现,8.2% 的提取物秸秆高度 有效抑制藻类微囊藻属的生长。这 细胞数量和叶绿素 a 的量在 治疗。两个值都连续下降,直到试验结束。

这是因为数字 (8.2%) 带有小数和 %。

我一直在试图弄清楚如何捕捉这些物品,但需要一些帮助来为我指明正确的方向。我不只是想捕捉第一句话。我想捕获 N 个可能包含多个句子的单词并返回完整的句子。

【问题讨论】:

  • 试试[\s\S]*?(?=\. |!|\?)
  • 我会稍微改变一下@ran_0315,[\s\S]*?(?=\.\s|!|\?)
  • 你需要写信,regex = /(?:\w+[.?!]?\s+){10}(?:\w+,?\s+)*?\w+[.?!]/。我在您的earlier question 中指出,当字符串包含 10 个单词且最后一个单词后跟标点符号时,此正则表达式不起作用:"a1 a2 a3 a4 a5 a6 a7 a8 a9 a10."[regex] #=> nil

标签: ruby regex


【解决方案1】:
r = /
    (?:           # begin a non-capture group
      (?:           # begin a non-capture group
        \p{Alpha}+  # match one or more letters
      |           # or
        \-?       # optionally match a minus sign
        (?:       # begin non-capture group
          \d+     # match one or more digits
        |         # or
          \d+     # match one or more digits
          \.      # match a decimal point
          \d+     # match one or more digits
        )         # end non-capture group
        %?        # optionally match a percentage character
      )           # end non-capture group
      [,;:.!?]?   # optionally ('?' following ']') match a punctuation char
      [ ]+        # match one or more spaces      
    )             # end non-capture group
    {9,}?         # execute the preceding non-capture group at least 14 times, lazily ('?')
    (?:           # begin a non-capture group
      \p{Alpha}+  # match one or more letters
      |           # or
      \-?         # optionally match a minus sign
        (?:       # begin non-capture group
          \d+     # match one or more digits
        |         # or
          \d+     # match one or more digits
          \.      # match a decimal point
          \d+     # match one or more digits
        )         # end non-capture group
      %?          # optionally match a percentage character
    )             # end non-capture group  
    [.!?]         # match one of the three punctuation characters
    (?!\S)        # negative look-ahead: do not match a non-whitespace char
    /x            # free-spacing regex definition mode

text 等于您要检查的段落(“Therapy extract straw...end of the trial.”)

然后

text[r]
  #=> "Therapy extract straw and chitosan from...the growth of algae Microcystis spp."

我们可以如下简化正则表达式的构造(并避免重复位)。

def construct_regex(min_nbr_words)
  common_bits = /(?:\p{Alpha}+|\-?(?:\d+|\d+\.\d+)%?)/
  /(?:#{common_bits}[,;:.!?]? +){#{min_nbr_words},}?#{common_bits}[.!?](?!\S)/
end

r = construct_regex(10)
  #=> /(?:(?-mix:\p{Alpha}+|\-?(?:\d+|\d+\.\d+)%?)[,;:.!?]? +){10,}?(?-mix:\p{Alpha}+|\-?(?:\d+|\d+\.\d+)%?)[.!?](?!\S)/

如果允许匹配诸如"ab2.3e%""2.3.2%" 之类的无意义单词,则可以简化此正则表达式。按照目前的定义,正则表达式不会匹配这些词。

【讨论】:

  • 感谢 Cary 令人难以置信的解释。这也很有效。唯一的问题是 Y.M.A. 等首字母缩略词中的句点。或琼斯先生。它将这些视为句子的结尾。
  • 关于"Y.M.A.",假设它是"Y.M.A. Limited is..."。我们知道第三个句号不是句子的结尾,因为前两个句号的存在。以可能在正则表达式中提供的相当大的复杂性为代价,但我认为没有办法处理"Mr. Jones"。这并不是正则表达式的真正限制,而是一个逻辑问题。作为人类,我们知道"Mr." 不会结束一个句子,但很难用可以翻译成正则表达式的词来解释原因。
【解决方案2】:

试试这个,(?:\S+[,.?!]?\s+){1,200}[\s\S]*?(\. |!|\?)

这将匹配 N 个字符。

如果第 N 个字符没有结束一个句子,那么它将匹配到前一个句子。 N 应该被称为{1, N}

Regex

【讨论】:

  • 第一句话的效果很好。我正在尝试做的是在一段中捕获 N 个单词而不会得到不完整的句子。
  • N 的值是多少?
  • 不超过 280 字
  • 我将正则表达式更改为:(?:\S+\s+){1,200}(?:[^.!?]|\.(?!\s))*. 您可以删除 [,.?!]? 部分,因为它们包含在 \S 字符中。您可以将[\s\S]*? 替换为.*?,但由于我不喜欢懒惰的重复,因此我将其替换为(?:[^.!?]|\.(?!\s))*,后跟. 以捕获句子的最后一个字符。
  • @Johan,如果你的正则表达式用{1,200} 替换为{1,10}r"a1 a2 a3 a4 a5. a6 a7 a8 a9 a10. a11."[r] #=> "a1 a2 a3 a4 a5. a6 a7 a8 a9 a10. a11."。我认为应该返回"a1 a2 a3 a4 a5 a6 a7 a8 a9 a10.",因为它包含最少数量的句子,其组合字数至少为10
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-19
相关资源
最近更新 更多