[英]Get Group Match in re.sub in Python
In Perl, it is possible for me to do a substitution and capture a group match at the same time. 在Perl中,我可以进行替换并同时捕获组匹配。 eg
例如
my $string = "abcdef123";
$string =~ s/(\d+)//;
my $groupMatched = $1; # $groupMatched is 123
In Python, I can do the substitution using re.sub function as follows. 在Python中,我可以使用re.sub函数进行替换,如下所示。 However, I cannot find a way to capture the \\d+ group match without invoking another function re.match and performing an additional operation.
但是,我找不到一种方法来捕获\\ d +组匹配而不调用另一个函数re.match并执行其他操作。
string = "abcdef123"
string = re.sub("(\d+)", "", string)
Does anyone know how I can capture the "\\d+" matched value as a separate variable from the same re.sub operation? 有谁知道如何将“\\ d +”匹配值作为一个单独的变量从同一个re.sub操作中捕获? I tried the following command and it doesn't work.
我尝试了以下命令,但它不起作用。
print r'\1'
You can cheat and pass a function to re.sub
: 你可以作弊并将函数传递给
re.sub
:
results = []
def capture_and_kill(match):
results.append(match)
return ""
string = "abcdef123"
string = re.sub("(\d+)", capture_and_kill, string)
results[0].group(1)
# => '123'
You can do the following: 您可以执行以下操作:
sub_str = re.search("(\d+)", str).group(1)
Will find the "123" part. 会找到“123”部分。
Then you replace it: 然后你替换它:
str = str.replace(sub_str, "")
Note that if you have more than [0-9] sequence you'll need to use findall
and iterate manually on all matches. 请注意,如果序列超过[0-9],则需要使用
findall
并在所有匹配项上手动迭代。
below code tested under python 3.6. 下面在python 3.6下测试的代码。
test = "abcdef123"
resp = re.sub(r'\w+[A-Za-z](\d+)',r'\1',test)
print (resp)
123
To build on Marouns answer you can also do the following: 要建立Marouns答案,您还可以执行以下操作:
str = "foobarzoo"
match = re.search(r"foo(bar)", str)
Then replace it with the full match "foobar": 然后用完整的匹配“foobar”替换它:
str = str.replace(match.group(0), "")
The first group (within the brackets) "bar" is then accessible by: 然后可以通过以下方式访问第一组(括号内)“bar”:
match.group(1)
Using this method str will then finally equal "zoo" 使用这种方法str最终将等于“动物园”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.