【问题标题】:How to check for words that are not immediately followed by a keyword, how about words not surrounded by the keyword?如何检查没有紧跟关键字的单词,没有被关键字包围的单词呢?
【发布时间】:2020-11-06 08:27:25
【问题描述】:

我正在尝试查找不紧跟在the 之前的单词。

执行积极的回溯以获取关键字“the”(?<=the\W) 之后的单词。但是,我无法捕捉到“人”和“那个”,因为上述逻辑不适用于这些情况。

我无法处理前后没有关键字“the”的单词(例如,句子中的“that”和“people”)。

p = re.compile(r'(?<=the\W)\w+') 
m = p.findall('the part of the fair that attracts the most people is the fireworks')

print(m)

目前得到的输出是

'part','fair','most','fireworks'. 

编辑:

感谢您在下面提供的所有帮助。使用 cmets 中的以下建议,设法更新了我的代码。

p = re.compile(r"\b(?!the)(\w+)(\W\w+\Wthe)?")
m = p.findall('the part of the fair that attracts the most people is the fireworks')

这让我更接近我需要得到的输出。

更新的输出:

[('part', ' of the'), ('fair', ''),
 ('that', ' attracts the'), ('most', ''),
 ('people', ' is the'), ('fireworks', '')]

我只需要字符串('part','fair','that','most','people','fireworks')。 有什么建议吗?

【问题讨论】:

    标签: python python-re lookbehind


    【解决方案1】:

    我正在尝试查找不直接出现在 'the' 之前的单词。

    请注意,下面的代码没有使用re。

    words = 'the part of the fair that attracts the most people is the fireworks'
    words_list = words.split()
    words_not_before_the = []
    for idx, w in enumerate(words_list):
        if idx < len(words_list)-1 and words_list[idx + 1] != 'the':
            words_not_before_the.append(w)
    words_not_before_the.append(words_list[-1])
    print(words_not_before_the)
    

    输出

    ['the', 'part', 'the', 'fair', 'that', 'the', 'most', 'people', 'the', 'fireworks']
    

    【讨论】:

      【解决方案2】:

      使用正则表达式:

      import re
      m = re.sub(r'\b(\w+)\b the', 'the', 'the part of the fair that attracts the most people is the fireworks')
      print([word for word in m.split(' ') if not word.isspace() and word])
      

      输出:

      ['the', 'part', 'the', 'fair', 'that', 'the', 'most', 'people', 'the', 'fireworks']
      

      【讨论】:

        【解决方案3】:

        我正在尝试查找不立即出现在 the 之前的单词。

        试试这个:

        import re
        
        # The capture group (\w+) matches a word, that is followed by a word, followed by the word: "the"
        p = re.compile(r'(\w+)\W\w+\Wthe')
        m = p.findall('the part of the fair that attracts the most people is the fireworks')
        print(m)
        

        输出:

        ['part', 'that', 'people']
        

        【讨论】:

        • 好问题,我更新了我的答案以反映我试图回答的 OP 的什么问题。我猜摘要和问题细节之间的模棱两可让我只能部分理解问题。
        【解决方案4】:

        尝试旋转它,而不是找到不紧跟the的单词,消除紧跟the的所有出现

        import re
        test = "the part of the fair that attracts the most people is the fireworks"
        pattern = r"\s\w*\sthe|the\s"
        print(re.sub(pattern, "", test))
        

        输出:part fair that most people fireworks

        【讨论】:

          【解决方案5】:

          我终于解决了这个问题。谢谢大家!

          p = re.compile(r"\b(?!the)(\w+)(?:\W\w+\Wthe)?")
          m = p.findall('the part of the fair that attracts the most people is the fireworks')
          print(m)
          
          
          

          在第三组中添加了一个非捕获组'?:'。

          输出:

          ['part', 'fair', 'that', 'most', 'people', 'fireworks']
          

          【讨论】:

            猜你喜欢
            • 2013-10-09
            • 2022-01-07
            • 1970-01-01
            • 1970-01-01
            • 2015-09-30
            • 1970-01-01
            • 2011-10-01
            • 1970-01-01
            • 2022-01-06
            相关资源
            最近更新 更多