N
N
Niko True2014-04-23 15:53:37
Python
Niko True, 2014-04-23 15:53:37

How to check if a character is in quotes in python?

Actually, let's say I have a string, it can consist of absolutely any characters, but, for example, punctuation marks must be in quotation marks. How can I do this check?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Max, 2014-04-23
@AloneCoder

Regular
\"[^\"]+\"will find you everything that is enclosed in quotes

D
Dmitry Guketlev, 2014-04-23
@Yavanosta

Here is the regular:
Fires if there is an even number of quotes (quotes are balanced) and there are punctuation marks between the odd (opening) and even (closing) quotes. In the example, punctuation marks are only a period and a comma (in the center of the regular expression), but you can add your own.
Demo: regexr.com/38oft

D
Dmitry Guketlev, 2014-04-23
@Yavanosta

Here's a little simpler, catches punctuation marks outside of quotes:

^([^"]*("[^"]*")*[^"]*)*([,.])([^"]*("[^"]*")*[^"]*)*$

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question