简体   繁体   中英

Simplify regex/ exclude parts from match

I am struggling with a regex that should capture all comma's between two quotes:

I currently obtained

("[^",]+)(,)([^"]+")

Unfortunately, this matches all text/ digits such as "2132134,23132"

Can anyone help me to transform the regex to just include the comma as a match (2nd group)?

Another example doesn't work for my sample data, unfortunately.

,(?=[^"]*"[^"]*(?:"[^"]*"[^"]*)*$)

Say, you want to replace all commas between two quotes with a semi-colon.

Use this :

import re
def replit(z):
    return z.group().replace(',', ';')

s = 'one,"two,three",four,"five,six,seven"'
results = re.sub(r'"[^"]+"', replit, s)
print(results)

Results : one,"two;three",four,"five;six;seven"

Use re.findall and turn off the first and third capture groups:

inp = '"2132134,23132"'
matches = re.findall(r'(?:"[^",]+)(,)(?:[^"]+")', inp)
print(matches)  # [',']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM