Issue
Let's say, that I have:
string= '{'id': '1'}'
and now using strings like in Perl/sed I would like to get
string=id
(in Perl it would look like string=~s/\{\'([a-zA-Z0-9]*)\'.*$)/\1/
)
Could you please give me a little insight how to do that in python? I expect that the regex syntax will be similar, but I'm not sure about the python syntax and what imports should I use, I'm quite beginner in Python :) Thank you a lot :-)
Solution
In Python you'd use the re
module for regular expression operations. I modified your regular expression a bit, but generally, this is how regular expression replacement can be done in python:
>>> import re
>>> s = "{'id': '1'}"
>>> re.sub(r"{'([^\']*)'.*$", r'\1', string)
'id'
The sub()
function accepts the regex first, then the replacement and finally the string. The documentation of the re module has some more information:
http://docs.python.org/library/re.html
The r
prefix to the strings passed as arguments basically tells Python to treat them as "raw" strings, where most backslash escape sequences are not interpreted.
Answered By - Kasperle Answer Checked By - Katrina (WPSolving Volunteer)