如何像用python替换文本一样替换sed ?

时间:2021-09-09 16:48:32

I would like to enable all apt repositories in this file

我想在这个文件中启用所有apt存储库

cat /etc/apt/sources.list
## Note, this file is written by cloud-init on first boot of an instance                                                                                                            
## modifications made here will not survive a re-bundle.                                                                                                                            
## if you wish to make changes you can:                                                                                                                                             
## a.) add 'apt_preserve_sources_list: true' to /etc/cloud/cloud.cfg                                                                                                                
##     or do the same in user-data
## b.) add sources in /etc/apt/sources.list.d                                                                                                                                       
#                                                                                                                                                                                   

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to                                                                                                           
# newer versions of the distribution.                                                                                                                                               
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick main                                                                                                                   
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick main                                                                                                               

## Major bug fix updates produced after the final release of the                                                                                                                    
## distribution.                                                                                                                                                                    
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick-updates main                                                                                                           
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick-updates main                                                                                                       

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu                                                                                                         
## team. Also, please note that software in universe WILL NOT receive any                                                                                                           
## review or updates from the Ubuntu security team.                                                                                                                                 
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick universe                                                                                                               
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick universe                                                                                                           
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick-updates universe
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
# deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick multiverse
# deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick multiverse
# deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick-updates multiverse
# deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick-updates multiverse

## Uncomment the following two lines to add software from the 'backports'
## repository.
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
# deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick-backports main restricted universe multiverse
# deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ maverick-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu maverick partner
# deb-src http://archive.canonical.com/ubuntu maverick partner

deb http://security.ubuntu.com/ubuntu maverick-security main
deb-src http://security.ubuntu.com/ubuntu maverick-security main
deb http://security.ubuntu.com/ubuntu maverick-security universe
deb-src http://security.ubuntu.com/ubuntu maverick-security universe
# deb http://security.ubuntu.com/ubuntu maverick-security multiverse
# deb-src http://security.ubuntu.com/ubuntu maverick-security multiverse

With sed this is a simple sed -i 's/^# deb/deb/' /etc/apt/sources.list what's the most elegant ("pythonic") way to do this?

sed这是一个简单的sed -我的年代/ ^ # deb / deb / /etc/apt/sources.列出最优雅的方法是什么?

12 个解决方案

#1


17  

massedit.py (http://github.com/elmotec/massedit) does the scaffolding for you leaving just the regex to write. It's still in beta but we are looking for feedback.

massedit。py (http://github.com/elmotec/massedit)为您提供了框架,只留下regex供您编写。它仍在测试阶段,但我们正在寻找反馈。

python -m massedit -e "re.sub(r'^# deb', 'deb', line)" /etc/apt/sources.list

will show the differences (before/after) in diff format.

将以diff格式显示差异(之前/之后)。

Add the -w option to write the changes to the original file:

添加-w选项将更改写入原始文件:

python -m massedit -e "re.sub(r'^# deb', 'deb', line)" -w /etc/apt/sources.list

Alternatively, you can now use the api:

您也可以使用以下的api:

>>> import massedit
>>> filenames = ['/etc/apt/sources.list']
>>> massedit.edit_files(filenames, ["re.sub(r'^# deb', 'deb', line)"], dry_run=True)

#2


38  

You can do that like this:

你可以这样做:

with open("/etc/apt/sources.list", "r") as sources:
    lines = sources.readlines()
with open("/etc/apt/sources.list", "w") as sources:
    for line in lines:
        sources.write(re.sub(r'^# deb', 'deb', line))

The with statement ensures that the file is closed correctly, and re-opening the file in "w" mode empties the file before you write to it. re.sub(pattern, replace, string) is the equivalent of s/pattern/replace/ in sed/perl.

with语句确保文件被正确关闭,并在写入文件之前以“w”模式重新打开文件。子(模式、替换、字符串)相当于s/pattern/replace/ in sed/perl。

Edit: fixed syntax in example

编辑:固定语法的例子

#3


17  

Authoring a homegrown sed replacement in pure Python with no external commands or additional dependencies is a noble task laden with noble landmines. Who would have thought?

在没有外部命令或附加依赖项的纯Python中编写一个自定义的sed替换是一项崇高的任务,其中充满了高尚的地雷。谁会想到呢?

Nonetheless, it is feasible. It's also desirable. We've all been there, people: "I need to munge some plaintext files, but I only have Python, two plastic shoelaces, and a moldy can of bunker-grade Maraschino cherries. Help."

尽管如此,它是可行的。这也是可取的。我们都有过这样的经历,人们:“我需要读一些明文文件,但我只有Python、两条塑料鞋带和一罐发霉的bunker级Maraschino樱桃。”帮助。”

In this answer, we offer a best-of-breed solution cobbling together the awesomeness of prior answers without all of that unpleasant not-awesomeness. As plundra notes, David Miller's otherwise top-notch answer writes the desired file non-atomically and hence invites race conditions (e.g., from other threads and/or processes attempting to concurrently read that file). That's bad. Plundra's otherwise excellent answer solves that issue while introducing yet more – including numerous fatal encoding errors, a critical security vulnerability (failing to preserve the permissions and other metadata of the original file), and premature optimization replacing regular expressions with low-level character indexing. That's also bad.

在这个答案中,我们提供了一种最好的解决方案,将之前的答案拼凑在一起,而不需要所有那些令人不快的、不那么可怕的答案。正如普兰德拉所指出的,David Miller在其他方面的最佳答案是非原子地编写所需的文件,因此会导致竞争条件(例如,来自其他试图同时读取该文件的线程和/或进程)。这是不好的。除此之外,普兰德拉出色的答案解决了这个问题,同时还引入了更多的问题——包括大量致命的编码错误、一个关键的安全漏洞(未能保存原始文件的权限和其他元数据),以及用低级字符索引替换正则表达式的过早优化。这也是不好的。

Awesomeness, unite!

精彩,团结起来!

import re, shutil, tempfile

def sed_inplace(filename, pattern, repl):
    '''
    Perform the pure-Python equivalent of in-place `sed` substitution: e.g.,
    `sed -i -e 's/'${pattern}'/'${repl}' "${filename}"`.
    '''
    # For efficiency, precompile the passed regular expression.
    pattern_compiled = re.compile(pattern)

    # For portability, NamedTemporaryFile() defaults to mode "w+b" (i.e., binary
    # writing with updating). This is usually a good thing. In this case,
    # however, binary writing imposes non-trivial encoding constraints trivially
    # resolved by switching to text writing. Let's do that.
    with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_file:
        with open(filename) as src_file:
            for line in src_file:
                tmp_file.write(pattern_compiled.sub(repl, line))

    # Overwrite the original file with the munged temporary file in a
    # manner preserving file attributes (e.g., permissions).
    shutil.copystat(filename, tmp_file.name)
    shutil.move(tmp_file.name, filename)

# Do it for Johnny.
sed_inplace('/etc/apt/sources.list', r'^\# deb', 'deb')

#4


12  

This is such a different approach, I don't want to edit my other answer. Nested with since I don't use 3.1 (Where with A() as a, B() as b: works).

这是一个如此不同的方法,我不想编辑我的另一个答案。嵌套因为我不使用3.1(其中A()作为A, B()作为B:工作)。

Might be a bit overkill to change sources.list, but I want to put it out there for future searches.

更改源代码可能有点过分。列表,但是我想把它放在那里以便以后搜索。

#!/usr/bin/env python
from shutil   import move
from tempfile import NamedTemporaryFile

with NamedTemporaryFile(delete=False) as tmp_sources:
    with open("sources.list") as sources_file:
        for line in sources_file:
            if line.startswith("# deb"):
                tmp_sources.write(line[2:])
            else:
                tmp_sources.write(line)

move(tmp_sources.name, sources_file.name)

This should ensure no race conditions of other people reading the file. Oh, and I prefer str.startswith(...) when you can do without a regexp.

这应该确保其他读取文件的人没有竞争条件。哦,我更喜欢str.startswith(…),当你不需要regexp时。

#5


5  

If you are using Python3 the following module will help you: https://github.com/mahmoudadel2/pysed

如果您正在使用Python3,下面的模块将会帮助您:https://github.com/mahmoudadel2/pysed

wget https://raw.githubusercontent.com/mahmoudadel2/pysed/master/pysed.py

Place the module file into your Python3 modules path, then:

将模块文件放到Python3模块路径中,然后:

import pysed
pysed.replace(<Old string>, <Replacement String>, <Text File>)
pysed.rmlinematch(<Unwanted string>, <Text File>)
pysed.rmlinenumber(<Unwanted Line Number>, <Text File>)

#6


3  

Try https://pypi.python.org/pypi/pysed

试试https://pypi.python.org/pypi/pysed

pysed -r '# deb' 'deb' /etc/apt/sources.list

列表

#7


2  

Not sure about elegant, but this ought to be pretty readable at least. For a sources.list it's fine to read all the lines before hand, for something larger you might want to change "in place" while looping through it.

不确定是否优雅,但这至少应该是相当可读的。对于一个来源。列出可以提前阅读所有的行,对于更大的内容,您可能希望在循环遍历时更改“到位”。

#!/usr/bin/env python
# Open file for reading and writing
with open("sources.list", "r+") as sources_file:
    # Read all the lines
    lines = sources_file.readlines()

    # Rewind and truncate
    sources_file.seek(0)
    sources_file.truncate()

    # Loop through the lines, adding them back to the file.
    for line in lines:
        if line.startswith("# deb"):
            sources_file.write(line[2:])
        else:
            sources_file.write(line)

EDIT: Use with-statement for better file-handling. Also forgot to rewind before truncate before.

编辑:使用with-statement进行更好的文件处理。在截断之前也忘了倒带。

#8


2  

You could do something like:

你可以这样做:

p = re.compile("^\# *deb", re.MULTILINE)
text = open("sources.list", "r").read()
f = open("sources.list", "w")
f.write(p.sub("deb", text))
f.close()

Alternatively (imho, this is better from organizational standpoint) you could split your sources.list into pieces (one entry/one repository) and place them under /etc/apt/sources.list.d/

另一种选择(imho,从组织的角度来说更好)是你可以将你的资源分开。将它们列成块(一个条目/一个存储库),并将它们放在/etc/ apt/sourcs .list.d/下

#9


2  

If you really want to use a sed command without installing a new Python module, you could simply do the following:

如果您真的想使用sed命令而不安装新的Python模块,您可以简单地执行以下操作:

import subprocess
subprocess.call("sed command")

#10


1  

Here's a one-module Python replacement for perl -p:

下面是perl -p的一个模块Python替换:

# Provide compatibility with `perl -p`

# Usage:
#
#     python -mloop_over_stdin_lines '<program>'

# In, `<program>`, use the variable `line` to read and change the current line.

# Example:
#
#         python -mloop_over_stdin_lines 'line = re.sub("pattern", "replacement", line)'

# From the perlrun documentation:
#
#        -p   causes Perl to assume the following loop around your
#             program, which makes it iterate over filename arguments
#             somewhat like sed:
# 
#               LINE:
#                 while (<>) {
#                     ...             # your program goes here
#                 } continue {
#                     print or die "-p destination: $!\n";
#                 }
# 
#             If a file named by an argument cannot be opened for some
#             reason, Perl warns you about it, and moves on to the next
#             file. Note that the lines are printed automatically. An
#             error occurring during printing is treated as fatal. To
#             suppress printing use the -n switch. A -p overrides a -n
#             switch.
# 
#             "BEGIN" and "END" blocks may be used to capture control
#             before or after the implicit loop, just as in awk.
# 

import re
import sys

for line in sys.stdin:
    exec(sys.argv[1], globals(), locals())
    try:
        print line,
    except:
        sys.exit('-p destination: $!\n')

#11


1  

I wanted to be able to find and replace text but also include matched groups in the content I insert. I wrote this short script to do that:

我希望能够查找和替换文本,但也要在插入的内容中包含匹配的组。我写了这个简短的脚本:

https://gist.github.com/turtlemonvh/0743a1c63d1d27df3f17

https://gist.github.com/turtlemonvh/0743a1c63d1d27df3f17

The key component of that is something that looks like like this:

它的关键部分是这样的

print(re.sub(pattern, template, text).rstrip("\n"))

Here's an example of how that works:

这里有一个例子说明这是如何工作的:

# Find everything that looks like 'dog' or 'cat' followed by a space and a number
pattern = "((cat|dog) (\d+))"

# Replace with 'turtle' and the number. '3' because the number is the 3rd matched group.
# The double '\' is needed because you need to escape '\' when running this in a python shell
template = "turtle \\3"

# The text to operate on
text = "cat 976 is my favorite"

Calling the above function with this yields:

使用此函数调用上述函数会得到:

turtle 976 is my favorite

#12


0  

Python has got a regex module (import re) . why you dont want to use it as done in perl. It has got all the features of a perl regex

Python有一个regex模块(import re)。为什么不像在perl中那样使用它呢?它具有perl regex的所有特性

#1


17  

massedit.py (http://github.com/elmotec/massedit) does the scaffolding for you leaving just the regex to write. It's still in beta but we are looking for feedback.

massedit。py (http://github.com/elmotec/massedit)为您提供了框架,只留下regex供您编写。它仍在测试阶段,但我们正在寻找反馈。

python -m massedit -e "re.sub(r'^# deb', 'deb', line)" /etc/apt/sources.list

will show the differences (before/after) in diff format.

将以diff格式显示差异(之前/之后)。

Add the -w option to write the changes to the original file:

添加-w选项将更改写入原始文件:

python -m massedit -e "re.sub(r'^# deb', 'deb', line)" -w /etc/apt/sources.list

Alternatively, you can now use the api:

您也可以使用以下的api:

>>> import massedit
>>> filenames = ['/etc/apt/sources.list']
>>> massedit.edit_files(filenames, ["re.sub(r'^# deb', 'deb', line)"], dry_run=True)

#2


38  

You can do that like this:

你可以这样做:

with open("/etc/apt/sources.list", "r") as sources:
    lines = sources.readlines()
with open("/etc/apt/sources.list", "w") as sources:
    for line in lines:
        sources.write(re.sub(r'^# deb', 'deb', line))

The with statement ensures that the file is closed correctly, and re-opening the file in "w" mode empties the file before you write to it. re.sub(pattern, replace, string) is the equivalent of s/pattern/replace/ in sed/perl.

with语句确保文件被正确关闭,并在写入文件之前以“w”模式重新打开文件。子(模式、替换、字符串)相当于s/pattern/replace/ in sed/perl。

Edit: fixed syntax in example

编辑:固定语法的例子

#3


17  

Authoring a homegrown sed replacement in pure Python with no external commands or additional dependencies is a noble task laden with noble landmines. Who would have thought?

在没有外部命令或附加依赖项的纯Python中编写一个自定义的sed替换是一项崇高的任务,其中充满了高尚的地雷。谁会想到呢?

Nonetheless, it is feasible. It's also desirable. We've all been there, people: "I need to munge some plaintext files, but I only have Python, two plastic shoelaces, and a moldy can of bunker-grade Maraschino cherries. Help."

尽管如此,它是可行的。这也是可取的。我们都有过这样的经历,人们:“我需要读一些明文文件,但我只有Python、两条塑料鞋带和一罐发霉的bunker级Maraschino樱桃。”帮助。”

In this answer, we offer a best-of-breed solution cobbling together the awesomeness of prior answers without all of that unpleasant not-awesomeness. As plundra notes, David Miller's otherwise top-notch answer writes the desired file non-atomically and hence invites race conditions (e.g., from other threads and/or processes attempting to concurrently read that file). That's bad. Plundra's otherwise excellent answer solves that issue while introducing yet more – including numerous fatal encoding errors, a critical security vulnerability (failing to preserve the permissions and other metadata of the original file), and premature optimization replacing regular expressions with low-level character indexing. That's also bad.

在这个答案中,我们提供了一种最好的解决方案,将之前的答案拼凑在一起,而不需要所有那些令人不快的、不那么可怕的答案。正如普兰德拉所指出的,David Miller在其他方面的最佳答案是非原子地编写所需的文件,因此会导致竞争条件(例如,来自其他试图同时读取该文件的线程和/或进程)。这是不好的。除此之外,普兰德拉出色的答案解决了这个问题,同时还引入了更多的问题——包括大量致命的编码错误、一个关键的安全漏洞(未能保存原始文件的权限和其他元数据),以及用低级字符索引替换正则表达式的过早优化。这也是不好的。

Awesomeness, unite!

精彩,团结起来!

import re, shutil, tempfile

def sed_inplace(filename, pattern, repl):
    '''
    Perform the pure-Python equivalent of in-place `sed` substitution: e.g.,
    `sed -i -e 's/'${pattern}'/'${repl}' "${filename}"`.
    '''
    # For efficiency, precompile the passed regular expression.
    pattern_compiled = re.compile(pattern)

    # For portability, NamedTemporaryFile() defaults to mode "w+b" (i.e., binary
    # writing with updating). This is usually a good thing. In this case,
    # however, binary writing imposes non-trivial encoding constraints trivially
    # resolved by switching to text writing. Let's do that.
    with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_file:
        with open(filename) as src_file:
            for line in src_file:
                tmp_file.write(pattern_compiled.sub(repl, line))

    # Overwrite the original file with the munged temporary file in a
    # manner preserving file attributes (e.g., permissions).
    shutil.copystat(filename, tmp_file.name)
    shutil.move(tmp_file.name, filename)

# Do it for Johnny.
sed_inplace('/etc/apt/sources.list', r'^\# deb', 'deb')

#4


12  

This is such a different approach, I don't want to edit my other answer. Nested with since I don't use 3.1 (Where with A() as a, B() as b: works).

这是一个如此不同的方法,我不想编辑我的另一个答案。嵌套因为我不使用3.1(其中A()作为A, B()作为B:工作)。

Might be a bit overkill to change sources.list, but I want to put it out there for future searches.

更改源代码可能有点过分。列表,但是我想把它放在那里以便以后搜索。

#!/usr/bin/env python
from shutil   import move
from tempfile import NamedTemporaryFile

with NamedTemporaryFile(delete=False) as tmp_sources:
    with open("sources.list") as sources_file:
        for line in sources_file:
            if line.startswith("# deb"):
                tmp_sources.write(line[2:])
            else:
                tmp_sources.write(line)

move(tmp_sources.name, sources_file.name)

This should ensure no race conditions of other people reading the file. Oh, and I prefer str.startswith(...) when you can do without a regexp.

这应该确保其他读取文件的人没有竞争条件。哦,我更喜欢str.startswith(…),当你不需要regexp时。

#5


5  

If you are using Python3 the following module will help you: https://github.com/mahmoudadel2/pysed

如果您正在使用Python3,下面的模块将会帮助您:https://github.com/mahmoudadel2/pysed

wget https://raw.githubusercontent.com/mahmoudadel2/pysed/master/pysed.py

Place the module file into your Python3 modules path, then:

将模块文件放到Python3模块路径中,然后:

import pysed
pysed.replace(<Old string>, <Replacement String>, <Text File>)
pysed.rmlinematch(<Unwanted string>, <Text File>)
pysed.rmlinenumber(<Unwanted Line Number>, <Text File>)

#6


3  

Try https://pypi.python.org/pypi/pysed

试试https://pypi.python.org/pypi/pysed

pysed -r '# deb' 'deb' /etc/apt/sources.list

列表

#7


2  

Not sure about elegant, but this ought to be pretty readable at least. For a sources.list it's fine to read all the lines before hand, for something larger you might want to change "in place" while looping through it.

不确定是否优雅,但这至少应该是相当可读的。对于一个来源。列出可以提前阅读所有的行,对于更大的内容,您可能希望在循环遍历时更改“到位”。

#!/usr/bin/env python
# Open file for reading and writing
with open("sources.list", "r+") as sources_file:
    # Read all the lines
    lines = sources_file.readlines()

    # Rewind and truncate
    sources_file.seek(0)
    sources_file.truncate()

    # Loop through the lines, adding them back to the file.
    for line in lines:
        if line.startswith("# deb"):
            sources_file.write(line[2:])
        else:
            sources_file.write(line)

EDIT: Use with-statement for better file-handling. Also forgot to rewind before truncate before.

编辑:使用with-statement进行更好的文件处理。在截断之前也忘了倒带。

#8


2  

You could do something like:

你可以这样做:

p = re.compile("^\# *deb", re.MULTILINE)
text = open("sources.list", "r").read()
f = open("sources.list", "w")
f.write(p.sub("deb", text))
f.close()

Alternatively (imho, this is better from organizational standpoint) you could split your sources.list into pieces (one entry/one repository) and place them under /etc/apt/sources.list.d/

另一种选择(imho,从组织的角度来说更好)是你可以将你的资源分开。将它们列成块(一个条目/一个存储库),并将它们放在/etc/ apt/sourcs .list.d/下

#9


2  

If you really want to use a sed command without installing a new Python module, you could simply do the following:

如果您真的想使用sed命令而不安装新的Python模块,您可以简单地执行以下操作:

import subprocess
subprocess.call("sed command")

#10


1  

Here's a one-module Python replacement for perl -p:

下面是perl -p的一个模块Python替换:

# Provide compatibility with `perl -p`

# Usage:
#
#     python -mloop_over_stdin_lines '<program>'

# In, `<program>`, use the variable `line` to read and change the current line.

# Example:
#
#         python -mloop_over_stdin_lines 'line = re.sub("pattern", "replacement", line)'

# From the perlrun documentation:
#
#        -p   causes Perl to assume the following loop around your
#             program, which makes it iterate over filename arguments
#             somewhat like sed:
# 
#               LINE:
#                 while (<>) {
#                     ...             # your program goes here
#                 } continue {
#                     print or die "-p destination: $!\n";
#                 }
# 
#             If a file named by an argument cannot be opened for some
#             reason, Perl warns you about it, and moves on to the next
#             file. Note that the lines are printed automatically. An
#             error occurring during printing is treated as fatal. To
#             suppress printing use the -n switch. A -p overrides a -n
#             switch.
# 
#             "BEGIN" and "END" blocks may be used to capture control
#             before or after the implicit loop, just as in awk.
# 

import re
import sys

for line in sys.stdin:
    exec(sys.argv[1], globals(), locals())
    try:
        print line,
    except:
        sys.exit('-p destination: $!\n')

#11


1  

I wanted to be able to find and replace text but also include matched groups in the content I insert. I wrote this short script to do that:

我希望能够查找和替换文本,但也要在插入的内容中包含匹配的组。我写了这个简短的脚本:

https://gist.github.com/turtlemonvh/0743a1c63d1d27df3f17

https://gist.github.com/turtlemonvh/0743a1c63d1d27df3f17

The key component of that is something that looks like like this:

它的关键部分是这样的

print(re.sub(pattern, template, text).rstrip("\n"))

Here's an example of how that works:

这里有一个例子说明这是如何工作的:

# Find everything that looks like 'dog' or 'cat' followed by a space and a number
pattern = "((cat|dog) (\d+))"

# Replace with 'turtle' and the number. '3' because the number is the 3rd matched group.
# The double '\' is needed because you need to escape '\' when running this in a python shell
template = "turtle \\3"

# The text to operate on
text = "cat 976 is my favorite"

Calling the above function with this yields:

使用此函数调用上述函数会得到:

turtle 976 is my favorite

#12


0  

Python has got a regex module (import re) . why you dont want to use it as done in perl. It has got all the features of a perl regex

Python有一个regex模块(import re)。为什么不像在perl中那样使用它呢?它具有perl regex的所有特性