Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove All Commas Between Quotes [duplicate]

Tags:

python

regex

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?

like image 305
carloabelli Avatar asked Jul 12 '16 18:07

carloabelli


People also ask

How do you get rid of double quotes in text?

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)).

How do you remove all commas in a string?

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.

How do I remove double quotes in notepad?

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.

How do you remove quotation marks from a string?

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.


1 Answers

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
  • So (?!...) asserts that we don't have even number of quotes ahead thus matching commas inside the quoted string only.
like image 84
anubhava Avatar answered Oct 14 '22 07:10

anubhava