65.9K
CodeProject is changing. Read more.
Home

Replace All Spaces (Or Any Other Character) Between Quotes in Python

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.40/5 (2 votes)

Jan 25, 2015

GPL3

1 min read

viewsIcon

16326

Replace all spaces (or any other character) between quotes in Python

This is an oddly specific post, but I recently had this situation come up and solving it was actually not very straightforward. Specifically, I was writing a program to interpret commands and this program happened to split different arguments by spaces. So what if you have something like this:

FTP CD “This is a file path”\”that uses quotes”\”and was a pain in my bum”

Well, I needed to get rid of the spaces temporarily between just the stuff in quotes. Here’s how  I did it:

# Use a regular expression to find any parts within quotes
string = "some string"\"which has quotes"\"and you don't want spaces or something"
for quoted_part in re.findall(r'\"(.+?)\"', string):
    # For each part that is within quotes replace the spaces with the pattern *#*#*#* or whatever you want
    string = string.replace(quoted_part, quoted_part.replace(" ", "*#*#*#*"))

Here's what the sample IO would look like:

Input going in: "some string"\"which has quotes"\"and you don't want spaces or something"

String after 1st iteration of the loop: "some*#*#*#*string"\"
which has quotes"\"and you don't want spaces or something"

2nd iteration: "some*#*#*#*string"\"which*#*#*#*has*#*#*#*quotes"\"
and you don't want spaces or something"

3rd iteration: "some*#*#*#*string"\"which*#*#*#*has*#*#*#*quotes"\"
and*#*#*#*you*#*#*#*don't*#*#*#*want*#*#*#*spaces*#*#*#*or*#*#*#*something"

Here’s How It Works

The Findall Method

In Python, the regular expression method findall method returns a list of all of the matched things. Here’s a good example I used from thegeekstuff:

>>> re.findall(r'dog', 'dog cat dog')
['dog', 'dog']
>>> re.findall(r'cat', 'dog cat dog')
['cat']

The Iterator

So what my code does is use the “for x in y” iterator construct to iterate over each instance of something found in quotes. For example, using my original command “This is a file path”\”that uses quotes”\”and was a pain in my bum” would have returned a list.

[‘This is a file path’, ‘that uses quotes’, ‘and was a pain in my bum’]

and the program will iterate over each of those things.

The Regular Expression

Here’s the regular expression I used: \”(.+?)\”

  • \”  means find a single quotation
  • . matches anything
  • + means for the preceding character, match anything with one or more instances (This basically means that there MUST be at least one thing between the quotes.)
  • ? in this context prevents the + from being too greedy

The Replace

string= string.replace(quoted_part, quoted_part.replace(" ", "*#*#*#*"))

This part is fairly straightforward. It just finds the quoted part within the string and replaces the characters in it with whatever you want.

Hopefully this saves someone some time.