简体   繁体   中英

Python like sed using regexes

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 :-)

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.

First of all, I agree with @PenguinCoder: since this is valid JSON, you should really think about just using the Python support for handling JSON.

I went to Google and typed in the keywords: Python regular expressions

Here are the top two hits:

http://docs.python.org/library/re.html

http://docs.python.org/howto/regex.html

If you read them you will find the answer.

Here's working code:

import re

s = '''string= "{'id': '1'}"'''

pat = re.compile(r"\s*([^=]+)\s*=[\s'\"]*{\s*'([^']+)'")

m = pat.match(s)

if m is not None:
    id = m.group(1)
    name = m.group(2)
    result = "%s=%s" % (id, name)
    # note: could also do this: result = "%s=%s" % m.groups()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM