【问题标题】:Pattern extract using Regex in Python在 Python 中使用正则表达式提取模式
【发布时间】:2021-06-27 22:46:33
【问题描述】:

我正在尝试在 python 中使用正则表达式进行单词提取,因为我是初学者并且没有正则表达式经验我希望你帮助我,我有这个字符串:

Deadline for NSF-BSF programs in Elementary Particle Physics – Theory; Particle Astrophysics and Cosmology – Theory; Quantum Information Science (NSF deadline is Dec. 14)

我希望输出是这个词中的区域或研究列表,所以输出应该是:

[Elementary Particle Physics, Particle Astrophysics and Cosmology, Quantum Information Science]

任何人都可以使用re.findall() 给出正则表达式来识别这种模式。 提前致谢!

【问题讨论】:

    标签: python regex nlp


    【解决方案1】:

    假设 (1) ' in ' 是您感兴趣的单词开始的指示符,并且 (2) 所有区域由 ';' 分隔,并且 (3) 所有区域以 @987654323 结尾@ 或括号中的内容,我们可以获得您要查找的列表。注意:如果以下代码要按预期工作,这些假设必须在所有输入源中保持一致。

    import re
    
    src = "Deadline for NSF-BSF programs in Elementary Particle Physics - Theory; " \
          "Particle Astrophysics and Cosmology - Theory; " \
          "Quantum Information Science (NSF deadline is Dec. 14)"
    
    _, out = src.split(' in ')
    out = [re.split(r'( - Theory)|\(.*\)', o)[0].strip() for o in out.split(';')]
    
    print(out)
    

    out:

    ['Elementary Particle Physics',
     'Particle Astrophysics and Cosmology',
     'Quantum Information Science']
    

    【讨论】:

    • 非常感谢,我很感激,我想要的,它就像一个魅力,因为所有的文本模式都和你之前假设的一样。
    猜你喜欢
    • 2017-05-13
    • 1970-01-01
    • 2021-07-30
    • 2021-06-11
    • 1970-01-01
    • 2018-07-29
    • 2015-11-21
    • 2019-07-17
    相关资源
    最近更新 更多