【问题标题】:Regex case sensitive (?-i) not working in jupyter notebook正则表达式区分大小写(?-i)在 jupyter notebook 中不起作用
【发布时间】:2023-01-18 15:16:34
【问题描述】:

我正在尝试从文本中提取公司名称。 示范文本:

“最大的公司 Abc Private Company Ltd.(批发)。”

使用正则表达式:

\b(?:(?-i)[A-Z][a-zA-Z()\.]*\s){2,5}

它正确识别了https://regexr.com/中的公司名称

但是当我在 jupyter notebook 中尝试相同的操作时,出现错误。

combined_df['company'] = combined_df['subject_link_text'].str.findall(r"\b(?:(?-i)[A-Z][a-zA-Z()\.]*\s){2,5}")

错误:

感谢任何帮助。提前致谢。

【问题讨论】:

    标签: python regex jupyter-notebook jupyter case-sensitive


    【解决方案1】:

    我认为不区分大小写的标志是(?i),而不是(?-i)。尝试以下操作:

    combined_df['company'] = combined_df['subject_link_text'].str.findall(r"(?:(?i)[A-Z][a-zA-Z().]*s){2,5}")
    

    或者,只需将 flags 选项与 re.I 一起使用即可不区分大小写:

    combined_df['company'] = combined_df['subject_link_text'].str.findall(r"(?:[A-Z][a-zA-Z().]*s){2,5}", flags=re.I)
    

    【讨论】:

      【解决方案2】:

      TBH 标志在这里似乎是多余的,(?:[A-Z][a-zA-Z().]*s){2,5} 应该可以解决问题 - 检查@regex101

      combined_df['company'] = combined_df['subject_link_text'].str.findall(r"(?:[A-Z][a-zA-Z().]*s){2,5}")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-22
        • 2017-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-18
        • 1970-01-01
        相关资源
        最近更新 更多