Regular Expressions - how to replace a character within quotes

前端 未结 3 1682
执念已碎
执念已碎 2020-12-16 03:16

Hello regular expression experts,

There has never been a string manipulation problem I couldn\'t resolve with regular expressions until now, at least in an elegant m

3条回答
  •  不思量自难忘°
    2020-12-16 03:39

    Excuse me if you're not using Python, in which is the following code. I didn't see any indication of which language you use. Anyway, I think the code is perfectly understandable.

    import re
    
    ch = '''0,"section1","(7) Delivery of 'certificate' outside the United States prohibited.
    Since both section 339 of the 1940 statute, 68/ and section 341 of the present law are explicit
    in their statement that the certificate shall be furnished the citizen, only if such individual
    is at the time within the United States, it is clear that the document could not and cannot be
    delivered outside the United States.",http://www.google.com/
    
    1,"section2",,http://www.google.com/
    
    2,"section3",",,",http://www.google.com/
    '''
    
    poto = re.compile('("[^"]+")')
    
    def comma_replacement(match):
        return match.group().replace(',','_')
    
    print poto.sub(comma_replacement , ch)
    

    This method keeps the 2 adjacent commas in the line

    1,"section2",,http://www.google.com/

    unchanged. Is it the right thing you want ?

提交回复
热议问题