正则表达式来获取括号内的单词

2024-09-22 16:41:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有正则表达式,它在括号之间打印,实际上我只需要特定的括号,我的意思是

car(skoda,audi)
bike(hayabusa)

我得到的输出是: 斯柯达 奥迪 隼鸟

为了得到括号中的汽车和自行车,我使用了:(r'^(\S+)\((.*)\)$')

但我只需要在“车(…)”里找到车,具体来说,怎么办?你知道吗

我试过:(r'^car(\S+)\((.*)\)$')

我只需要skoda,audi而不是隼鸟

我没有得到输出

要使用的编码:

class Group:
    def __init__(self):
        self.members = []
        self.text = []

with open('text1.txt') as f:
    groups = collections.defaultdict(Group)
    group_pattern = re.compile(r'^(\S+)\((.*)\)$')  #<=here i am using
    current_group = None

    for line in f:
        line = line.strip()
        m = group_pattern.match(line)
        if m:    # this is a group definition line
            group_name, group_members = m.groups()
            groups[group_name].members.extend(group_members.split(','))
            current_group = group_name
        else:
            if (current_group is not None) and (len(line) > 0):
                groups[current_group].text.append(line)

for group_name, group in groups.items():
    print "%s(%s)" % (group_name, ','.join(group.members))
    print '\n'.join(group.text)

Tags: textnameselfnonelinegroupcurrentcar
2条回答

你可以试试Positive Lookbehind & Lookahead

The lookaround actually matches characters, but then gives up the match, returning only the result: match or no match.

(?<=^car\().*(?=\)$)

online demo

或者使用findall从索引1中获取匹配的组

^car\((.*)\)$

online demo


示例代码:

import re
p = re.compile(ur'(?<=^car\().*(?=\)$)')
test_str = u"car(skoda,audi)"

re.findall(p, test_str)

你的代码怎么了?你知道吗

^car(\S+)\((.*)\)$

代码与预期字符串匹配的原因是

  • 您需要将(\S+)更改为(\S*),因为\S+执行贪婪操作匹配。那个它和最后一个匹配。因此不会发生捕获。你知道吗

最后你的正则表达式是

^car(\S*)\((.*)\)$

获取组索引2中的字符串。你知道吗

>>> import re
>>> s = """car(skoda,audi)
... bike(hayabusa)"""
>>> regex = re.compile(r'^car\S*\((.*)\)$', re.M)
>>> m = regex.findall(s)
>>> m
['skoda,audi']

相关问题 更多 >