简体   繁体   中英

replace a string with regular expression in python

I have been learning regular expression for a while but still find it confusing sometimes I am trying to replace all the

self.assertRaisesRegexp(SomeError,'somestring'):

to

self.assertRaiseRegexp(SomeError,somemethod('somestring'))

How can I do it? I am assuming the first step is fetch 'somestring' and modify it to somemethod('somestring') then replace the original 'somestring'

A better tool for this particular task is sed:

$ sed -i 's/\(self.assertRaisesRegexp\)(\(.*\),\(.*\))/\1(\2,somemethod(\3))/' *.py

sed will take care of the file I/O, renaming files, etc.

If you already know how to do the file manipulation, and iterating over lines in each file, then the python re.sub line will look like:

new_line = re.sub(r"(self.assertRaisesRegexp)\((.*),(.*)\)", 
                  r"\1(\2,somemethod(\3)", 
                  old_line)

here is your regular expression

#f is going to be your file in string form
re.sub(r'(?m)self\.assertRaisesRegexp\((.+?),((?P<quote>[\'"]).*?(?P=quote))\)',r'self.assertRaisesRegexp(\1,somemethod(\2))',f)

this will grab something that matches and replace it accordingly. It will also make sure that the quotation marks line up correctly by setting a reference in quote

there is no need to iterate over the file here either, the first statement "(?m)" puts it in multiline mode so it maps the regular expression over each line in the file. I have tested this expression and it works as expected!

test

>>> print f
this is some
multi line example that self.assertRaisesRegexp(SomeError,'somestring'):
and so on. there self.assertRaisesRegexp(SomeError,'somestring'): will be many
of these in the file and I am just ranting for example
here is the last one self.assertRaisesRegexp(SomeError,'somestring'): okay 
im done now
>>> print re.sub(r'(?m)self\.assertRaisesRegexp\((.+?),((?P<quote>[\'"]).*?(?P=quote))\)',r'self.assertRaisesRegexp(\1,somemethod(\2))',f)
this is some
multi line example that self.assertRaisesRegexp(SomeError,somemethod('somestring')):
and so on. there self.assertRaisesRegexp(SomeError,somemethod('somestring')): will be many
of these in the file and I am just ranting for example
here is the last one self.assertRaisesRegexp(SomeError,somemethod('somestring')): okay 
im done now

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