【问题标题】:Regular expression to extract number before/after word正则表达式提取单词之前/之后的数字
【发布时间】:2018-11-14 01:13:46
【问题描述】:

我有10000 描述,我想使用正则表达式来提取与短语“被捕”相关的数字。

例如:

"police arrests 4 people"
"7 people were arrested". 

数字范围为1-99。

我已经尝试了以下代码:

gen arrest= regexm(description, "(^[1-9][0-9]$)[ ]*(arrests|arrested)")

我不能简单地提取数字,因为描述中还提到了与逮捕无关的数字。

【问题讨论】:

    标签: regex stata


    【解决方案1】:

    你可以使用这个正则表达式:

    (?:([1-9]?[0-9])[a-zA-Z ]{0,20}(?:arrests|arrested))|(?:(?:arrests|arrested)[a-zA-Z ]{0,20}([1-9]?[0-9]))
    

    无论数字是在'arrests|arrested'之前还是之后,它都会将搜索分成2份。

    它会创建一个非捕获 Group,它匹配 1-9 的 number(可选)和 0-9 的数字。接下来是匹配0 - 20 of any letter 和空格(其他单词),然后再匹配'arrests OR arrested.,然后将其与相反的情况(数字最后出现)进行或运算。

    如果号码是来自“arrests|arrested”的within 20 chars,这将匹配。

    【讨论】:

      【解决方案2】:

      也许是这样的?

      (\d+)[^,.\d\n]+?(?=arrest|custody)|(?<=arrest|custody)[^,.\d\n]+?(\d+)
      

      Regex101

      请记住,这与数字的文本版本不匹配(即,有 5 人被捕) - 因此,如果需要,您必须将其合并。


      打破模式

      • (\d+)[^,.\d\n]+?(?=arrest|custody) 第一个选项,如果 # 出现在观察的术语之前
        • (\d+) 要捕获的号码,+ 一位或多位数字
        • [^,.\d\n]+? 匹配除逗号 ,、句点 .、数字 \d 或换行符 \n 之外的任何内容。这些防止 FPs 来自不同的句子(必须包含在同一个句子中) - +? 一次或多次(懒惰)
        • (?=arrest|custody) 积极前瞻检查任一单词:
      • (?&lt;=arrest|custody)[^,.\d\n]+?(\d+) 第二个选项,如果 # 出现在观察的条款之后
        • (?&lt;=arrest|custody) 正向后视检查单词是否在 # 之前
        • [^,.\d\n]+? 匹配除逗号 ,、句点 .、数字 \d 或换行符 \n 之外的任何内容。这些防止 FPs 来自不同的句子(必须包含在同一个句子中) - +? 一次或多次(懒惰)
        • (\d+) 要捕获的号码,+ 一位或多位数字

      杂记

      如果您想添加数字的文本表示,则可以将其合并到 (\d+) 捕获组中。

      如果除了arrested或custody之外,您还有其他要注意的条款,您可以将这些条款添加到两个环视组中

      【讨论】:

        【解决方案3】:

        以下对我有用(基于@PoulBak 想法的解决方案):

        clear
        
        input strL var1
        "This is 1 long string saying that police arrests 4 people"
        "3 news outlets today reported that 7 people were arrested"
        "several witnesses saw 5 people arrested and other 3 killed"
        end
        
        generate var2 = ustrregexs(0) if ustrregexm(var1, "(?:([1-9]?[0-9])[a-zA-Z ]{0,20}(?:arrests|arrested))|(?:(?:arrests|arrested)[a-zA-Z ]{0,20}([1-9]?[0-9]))")
        
        list
        
           +-------------------------------------------------------------------------------------+
           |                                                       var1                     var2 |
           |-------------------------------------------------------------------------------------|
        1. |  This is 1 long string saying that police arrests 4 people                arrests 4 |
        2. |  3 news outlets today reported that 7 people were arrested   7 people were arrested |
        3. | several witnesses saw 5 people arrested and other 3 killed        5 people arrested |
           +-------------------------------------------------------------------------------------+
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-26
          • 2018-01-17
          • 1970-01-01
          • 1970-01-01
          • 2017-12-13
          • 1970-01-01
          相关资源
          最近更新 更多