Given strings like:
"hello"
'hello'
I want to remove only first and last char if:
- They are the same
- They are
"or'
I.e., given 'hello' I’m expecting hello. Given 'hello" I’m not expecting it to change.
I was able to do this by reading first char and last char, validating they are the same + validating they are equal to ' or " and validating it’s not the the same index for char (because I don’t want this: ' to end up as the empty string). With all edge cases checking I ended with 10s of lines.
What’s your approach to solve this?
In simple words, Given a string in Python format I want to return its data and if it’s not valid to keep it as is.
>Solution :
Sounds like a job for regular expressions with groups:
import re
re.sub(r'^([\'"])(.*)(\1)$', r'\2', s)
Which reads as:
^– match the beginning of the string(['"])– either single or double quote (group 1)(.*)any (possibly, empty) sequence of characters in between (group 2)(\1)– the same character as in group 1$– end of the string
If the string matches the pattern above, replace it with the content of the group 2.
For example:
>>> s = re.sub(r'^([\'"])(.*)(\1)$', r'\2', "'hello'")
>>> print(s)
hello
An alternative way could be with ast.literal_eval(), but it won’t handle non-matching quotes.