简体   繁体   English

Python喜欢使用正则表达式进行sed

[英]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 现在使用像Perl / sed这样的字符串,我想

string=id

(in Perl it would look like string=~s/{\\'([a-zA-Z0-9] )\\'. $)/\\1/ ) (在Perl中,它看起来像是string =〜s / {\\'([a-zA-Z0-9] )\\'。 $)/ \\ 1 /)

Could you please give me a little insight how to do that in python? 您能否给我一些见识,如何在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 :-) 我希望regex语法相似,但是我不确定python语法以及应该使用什么导入,我是Python的初学者:)非常感谢:-)

In Python you'd use the re module for regular expression operations. 在Python中,您可以使用re模块进行正则表达式操作。 I modified your regular expression a bit, but generally, this is how regular expression replacement can be done in python: 我对您的正则表达式做了一些修改,但是通常,这是可以在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. sub()函数首先接受正则表达式,然后接受替换,最后接受字符串。 The documentation of the re module has some more information: http://docs.python.org/library/re.html re模块的文档提供了更多信息: 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. 作为参数传递的字符串的r前缀基本上告诉Python将其视为“原始”字符串,其中大多数反斜杠转义序列都不会被解释。

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. 首先,我同意@PenguinCoder:由于这是有效的JSON,因此您应该真正考虑只使用Python支持来处理JSON。

I went to Google and typed in the keywords: Python regular expressions 我去了Google并输入了关键字: Python regular expressions

Here are the top two hits: 以下是排名前两名的热门歌曲:

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

http://docs.python.org/howto/regex.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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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