How to remove the quotes of certain patterns in python string

I want to remove the quotes of certain patterns (numpy.*) in python string.
For example, I have a string

"['numpy.number', 'numpy.complex_', 'numpy.int8', 'randomStr']"

`I want to remove the inner quotes to

"[numpy.number, numpy._complex, numpy.int8, 'randomStr']"

How should I define the regex rules for it?

>Solution :

Perhaps something like the following regular expression substitution:

>>> import re
>>> s = "['numpy.number', 'numpy.complex_', 'numpy.int8', 'randomStr']"
>>> re.sub(r"'(numpy\.[_a-zA-Z][_a-zA-Z0-9]*)'", r'\1', s)
"[numpy.number, numpy.complex_, numpy.int8, 'randomStr']"
>>>

Leave a Reply