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:
string = "some string"\"which has quotes"\"and you don't want spaces or something"
for quoted_part in re.findall(r'\"(.+?)\"', string):
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.
Grant is a specialist in computer security and networking. He holds a bachelors degree in Computer Science and Engineering from the Ohio State University. Certs: CCNA, CCNP, CCDA, CCDP, Sec+, and GCIH.