【发布时间】:2014-02-14 08:03:38
【问题描述】:
我对 python 和整个编程还很陌生。只是关于学习我的ABC。比方说,我有一个这样的字符串。
s = "DEALER:'S up, Bubbless? BUBBLES: Hey. DEALER: Well, there you go. JUNKIE: Well, what you got?DEALER: I got some starters.";
我希望字符串在遇到结尾带有大写和冒号(:) 的单词时结束。然后创建一个新字符串来存储另一个字符串。对于上面的字符串,我会得到
s1 = "DEALER:'S up, Bubbless?"
s2 = "BUBBLES: Hey."
s3 = "DEALER: Well, there you go."
这是我的正则表达式代码
mystring = """
DEALER: 'S up, Bubbless?
BUBBLES: Hey.
DEALER: Well, there you go.
JUNKIE: Well, what you got?
DEALER: I got some starters. """
#[A-Z]+:.*?(?=[A-Z]+:|$)
#p = re.compile('([A-Z]*):')
p = re.compile('[A-Z]+:.*?(?=[A-Z]+:|$)')
s = set(p.findall(mystring))
我将如何遍历它以获取每个字符串?它只获取第一个字符串(即 DEALER: 'S up, Bubbless?)并停止。对不起,如果我听起来有点无能为力。对编程有点陌生。边学边学
【问题讨论】:
标签: python regex string string-matching