【问题标题】:How can I select a string using start and endpoints (characters)?如何使用开始和端点(字符)选择字符串?
【发布时间】:2014-08-30 15:37:24
【问题描述】:

如何在python中选择一个知道起点和终点的字符串?

如果字符串是:

Evelin said, "Hi Dude! How are you?" and no one cared!!

或者是这样的:

Jane said *aww! thats cute, we must try it!* John replied, "Okay!, but not now!!"

我想写的是一个从" "中选择的函数,而不是通过计算索引,
但只是从一个字符到另一个字符选择文本,

"Hi Dude! How are you?" 和 "Okay!, but not now!!"

那么我该怎么做呢?有内置函数吗?

我知道python中有一个内置函数可以获取给定字符的索引

即,

find("something")返回给定字符串在字符串中的索引。

还是需要遍历字符串?

我刚开始接触python,对于这样的小问题很抱歉。 python 2或3就可以了!!非常感谢!!

更新:

谢谢大家的回答,作为一个初学者,我只想坚持使用内置的split()函数quotes = string.split('"')[1::2],因为它很简单。谢谢你们。太爱了:)

【问题讨论】:

  • 引号应该是提取字符串的一部分吗?
  • 您不想要包含函数 str.index(str) ?????的解决方案
  • @martin 引号不应该是一部分,
  • @mlwn 我只是不想写一个带有静态索引的函数,动态索引没问题,如果我能找到某个随机用户输入的字符串 x 的索引。谢谢
  • @John ..检查下面的答案...没有静态索引..所有动态

标签: python string search find


【解决方案1】:
txt='''\
Evelin said, "Hi Dude! How are you?" and no one cared!!
Jane said *aww! thats cute, we must try it!* John replied, "Okay!, but not now!!"'''

import re

print re.findall(r'"([^"]+)"', txt)
# ['Hi Dude! How are you?', 'Okay!, but not now!!']

【讨论】:

    【解决方案2】:

    如果不想使用str.index(),可以使用regular expressions:

    import re
    
    quotes = re.findall('"([^"]*)"', string)
    

    您也可以轻松扩展它以从您的字符串中提取其他信息。

    或者:

    quotes = string.split('"')[1::2]
    

    并使用str.index():

    first = string.index('"')
    second = string.index('"', first+1)
    quote = string[first+1:second]
    

    【讨论】:

    • 我喜欢quotes = string.split('"')[1::2] 方法,它非常简单,无需任何导入即可完成。顺便说一句,这是什么 [1::2] ?谢谢你
    • 看看this。基本上,我从第一个元素开始,每隔一个元素取一个。如果字符串中只有一个引号,则可以使用 [1]。
    • 小心使用'"(.*?)"' 的正则表达式它won't match 多行引号,除非你使用re.S 标志。
    【解决方案3】:

    要按字符提取子字符串,在这些字符上拆分要容易得多; str.partition() 和 str.rpartition() 在给定字符串的第一次或最后一次出现时有效地拆分字符串:

    extracted = inputstring.partition('"')[-1].rpartition('"')[0]
    

    从头到尾的分区组合为您提供了可能的最大子字符串,并在其中留下任何嵌入的引号。

    演示:

    >>> inputstring = 'Evelin said, "Hi Dude! How are you?" and no one cared!!'
    >>> inputstring.partition('"')
    ('Evelin said, ', '"', 'Hi Dude! How are you?" and no one cared!!')
    >>> inputstring.rpartition('"')
    ('Evelin said, "Hi Dude! How are you?', '"', ' and no one cared!!')
    >>> inputstring.partition('"')[-1].rpartition('"')[0]
    'Hi Dude! How are you?'
    

    【讨论】:

      【解决方案4】:

      str.index(str2) 在str中找到str2的索引...最简单的方法!

      a = 'Evelin said, "Hi Dude! How are you?" and no one cared!!'
      print a[1+a.index("\""):1+a.index("\"")+a[a.index("\"")+1:].index("\"")]
      

      或者正如 Scorpion_God 提到的,你可以简单地使用单引号,如下所示

      print a[1+a.index('"'):1+a.index('"')+a[a.index('"')+1:].index('"')]
      

      这将导致:

      Hi Dude! How are you?
      

      报价将不包括在内!!!

      【讨论】:

      • @Scorpion_God,你总是可以使用更多变量并将表达式拆分为非常简单的表达式......我只是将所有内容组合在一行中
      • 我知道你是那种喜欢善待阅读他们代码的人的人之一。如果您使用单引号,则不需要所有这些反斜杠。
      • 另外,使用更多变量,就像我在回答中所做的那样,可以更清楚地了解您想要实现的目标。而且您无需多次致电str.index()。
      猜你喜欢
      • 1970-01-01
      • 2014-06-27
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 2018-11-14
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多