【问题标题】:Finding words after keyword in python在python中查找关键字后的单词
【发布时间】:2011-10-01 19:09:26
【问题描述】:

我想查找出现在关键字(由我指定和搜索)之后出现的单词并打印出结果。我知道我想用正则表达式来做,我也试过了,像这样:

import re
s = "hi my name is ryan, and i am new to python and would like to learn more"
m = re.search("^name: (\w+)", s)
print m.groups()

输出只是:

"is"

但我想获取“名称”之后的所有单词和标点符号。

【问题讨论】:

    标签: python regex matching keyword


    【解决方案1】:

    使用 "^name: (\w+)" 代替:

    "^name:(.*)"
    

    【讨论】:

      【解决方案2】:

      您可以(例如)将字符串与str.partition(separator) 分开,而不是使用正则表达式,如下所示:

      mystring =  "hi my name is ryan, and i am new to python and would like to learn more"
      keyword = 'name'
      before_keyword, keyword, after_keyword = mystring.partition(keyword)
      >>> before_keyword
      'hi my '
      >>> keyword
      'name'
      >>> after_keyword
      ' is ryan, and i am new to python and would like to learn more'
      

      不过,您必须单独处理不必要的空格。

      【讨论】:

      • 如果同一个句子中有多个'name'怎么办?那么你的解决方案就行不通了
      • 应该是mystring.partition(keyword),没有keyword周围的引号
      • 如果keyword在字符串中出现多次会给出虚假结果。
      • 非常感谢 :)
      • 太棒了!谢谢!
      【解决方案3】:

      关于你的输出你使用了什么:

      re.search("name (\w+)", s)
      

      你必须使用什么(匹配所有):

      re.search("name (.*)", s)
      

      【讨论】:

        【解决方案4】:

        你可以这样做

        s = "hi my name is ryan, and i am new to python and would like to learn more"
        s.split('name')
        

        这将拆分你的字符串并返回一个像这样的列表 ['hi my', 'is ryan, and i am new to python and would like to learn more']

        这取决于你想做什么,这可能会有所帮助。

        【讨论】:

          【解决方案5】:

          您的示例不起作用,但据我了解:

          regexp = re.compile("name(.*)$")
          print regexp.search(s).group(1)
          # prints " is ryan, and i am new to python and would like to learn more"
          

          这将打印“name”之后的所有内容,直到行尾。

          【讨论】:

            【解决方案6】:

            另一种选择...

               import re
               m = re.search('(?<=name)(.*)', s)
               print m.groups()
            

            【讨论】:

              【解决方案7】:

              这对你有用:作品名\s\w+\s(\w+)

              >>> s = 'hi my name is ryan, and i am new to python and would like to learn more'
              >>> m = re.search('name\s\w+\s(\w+)',s)
              >>> m.group(0)
              'name is ryan'
              >>>> m.group(1)
              'ryan'
              

              【讨论】:

                【解决方案8】:

                不使用正则表达式,你可以

                • 去除标点符号(考虑将所有内容都设为单一大小写,包括搜索词)

                • 将文本拆分为单个单词

                • 查找搜索词的索引

                • 从数组中获取单词(index + 1 用于后面的单词,index - 1 用于前面的单词)

                代码sn-p:

                import string
                s = 'hi my name is ryan, and i am new to python and would like to learn more'
                t = 'name'
                i = s.translate(string.maketrans("",""), string.punctuation).split().index(t)
                print s.split()[i+1]
                
                >> is
                

                对于多次出现,需要保存多个索引:

                import string
                s = 'hi my NAME is ryan, and i am new to NAME python and would like to learn more'
                t = 'NAME'
                il = [i for i, x in enumerate(s.translate(string.maketrans("",""), string.punctuation).split()) if x == t]
                print [s.split()[x+1] for x in il]
                
                >> ['is', 'python']
                

                【讨论】:

                  【解决方案9】:
                  import re
                  s = "hi my name is ryan, and i am new to python and would like to learn more"
                  m = re.search("^name: (\w+)", s)
                  
                  print m.group(1)
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2013-10-09
                    相关资源
                    最近更新 更多