python re.sub,只替换部分匹配 [英] python re.sub, only replace part of match

查看:53
本文介绍了python re.sub,只替换部分匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对python很陌生

我需要用一个正则表达式匹配所有情况并进行替换.这是一个示例子字符串 --> 所需的结果:

--><cross_sell>

我试图在我的代码中做到这一点:

myString = re.sub(r'\<[A-Za-z0-9_]+(\s[A-Za-z0-9_="\s]+)', "",我的字符串)

不是替换 <cross_sell 之后的所有内容,而是替换所有内容并只返回 '>'

有没有办法让 re.sub 只替换捕获组而不是整个模式?

解决方案

您可以使用替换组:

<预><代码>>>>my_string = '<cross_sell id="123" sale_type="456">--><cross_sell>'>>>re.sub(r'(\<[A-Za-z0-9_]+)(\s[A-Za-z0-9_="\s]+)', r"\1", my_string)'<cross_sell>--><cross_sell>'

请注意,我将第一组(您要保留的组)放在括号中,然后通过在替换字符串中使用 "\1" 修饰符(第一组)将其保留在输出中.

I am very new to python

I need to match all cases by one regex expression and do a replacement. this is a sample substring --> desired result:

<cross_sell id="123" sell_type="456"> --> <cross_sell>

i am trying to do this in my code:

myString = re.sub(r'\<[A-Za-z0-9_]+(\s[A-Za-z0-9_="\s]+)', "", myString)

instead of replacing everything after <cross_sell, it replaces everything and just returns '>'

is there a way for re.sub to replace only the capturing group instead of the entire pattern?

解决方案

You can use substitution groups:

>>> my_string = '<cross_sell id="123" sell_type="456"> --> <cross_sell>'
>>> re.sub(r'(\<[A-Za-z0-9_]+)(\s[A-Za-z0-9_="\s]+)', r"\1", my_string)
'<cross_sell> --> <cross_sell>'

Notice I put the first group (the one you want to keep) in parenthesis and then I kept that in the output by using the "\1" modifier (first group) in the replacement string.

这篇关于python re.sub,只替换部分匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆