Replacing repeated captures

后端 未结 4 1898
悲哀的现实
悲哀的现实 2021-02-18 13:11

This is sort of a follow-up to Python regex - Replace single quotes and brackets thread.

The task:

Sample input strings:

RSQ(nam         


        
4条回答
  •  悲哀的现实
    2021-02-18 13:37

    You can use re.findall() and a simple string formatting:

    >>> s = "SMT(name['BAKD DK'], name['A DKJ'], name['S QRT'])"
    >>> 
    >>> 'XYZ({})'.format(','.join(re.findall(r"'([^']+)'", s)))
    'XYZ(BAKD DK,A DKJ,S QRT)'
    

提交回复
热议问题