I'm trying to remove all commas that are inside quotes ("
) with python:
'please,remove all the commas between quotes,"like in here, here, here!"'
^ ^
I tried this, but it only removes the first comma inside the quotes:
re.sub(r'(".*?),(.*?")',r'\1\2','please,remove all the commas between quotes,"like in here, here, here!"')
Output:
'please,remove all the commas between quotes,"like in here here, here!"'
How can I make it remove all the commas inside the quotes?
Option 1: Remove any double quotes in a text string with replace('my string','"',''). This will substitute any instance of a double quote anywhere in the string with an empty string. Option 2: Remove the first and last character in a string with substring('my string',1,sub(length('my string'),2)).
To remove all commas from a string, call the replace() method, passing it a regular expression to match all commas as the first parameter and an empty string as the second parameter. The replace method will return a new string with all of the commas removed.
The easiest way to do this is to highlight one of the quotes, then select Search, then Replace. You will see the Find What field is already filled in with the quote you selected. I suggest have Search mode set to normal. Make sure the Replace With field is empty.
To remove double quotes from a string:Call the replace() method on the string. The replace method will replace each occurrence of a double quote with an empty string. The replace method will return a new string with all double quotes removed.
Assuming you don't have unbalanced or escaped quotes, you can use this regex based on negative lookahead:
>>> str = r'foo,bar,"foobar, barfoo, foobarfoobar"'
>>> re.sub(r'(?!(([^"]*"){2})*[^"]*$),', '', str)
'foo,bar,"foobar barfoo foobarfoobar"'
This regex will find commas if those are inside the double quotes by using a negative lookahead to assert there are NOT even number of quotes after the comma.
Note about the lookaead (?!...)
:
([^"]*"){2}
finds a pair of quotes(([^"]*"){2})*
finds 0 or more pair of quotes[^"]*$
makes sure we don't have any more quotes after last matched quote(?!...)
asserts that we don't have even number of quotes ahead thus matching commas inside the quoted string only.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With