English 中文(简体)
灰 像用reg子 s。
原标题:Python like sed using regexes

我要说的是:

string=  { id :  1 } 

并且现在使用Perl/sed的护卫,我想得到一些东西。

string=id

(单位:%) 页: 1

请让我略感了解一下,在座各位如何做到这一点? 我预计reg子会类似,但我不敢肯定 p子和我应该使用什么进口品,I m在沙尔开端: 感谢您:

最佳回答

在沙里,你经常使用<代码>re模块。 我对你的定期表述作了修改,但总体来说,这就是如何经常用字眼替换表达方式:

>>> 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 prefixing to the strings adopted as debate basic notess 用来将他们视为“草”扼杀物,在这种地方,大多数反弹越狱事件没有解释。

问题回答

首先,我同意@PenguinCoder:由于这是有效的初等人物,你应当真正思考如何仅仅利用斯图尔为处理JSON提供支持。

我去了谷歌,在关键词中打上了字:Python 常规表述

这里是头两点:

rel=“nofollow>http://docs.python.org/library/re.html

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

如果你们读了这些话,你将找到答案。

这里的工作守则:

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




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签