Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python re.sub()通配符字符串替换_Python_Regex - Fatal编程技术网

Python re.sub()通配符字符串替换

Python re.sub()通配符字符串替换,python,regex,Python,Regex,这是源字符串,它有两个通配符和,都是任意位数 SomeText<1>_<2>.value SomeText\uu0.value 我需要将上面的字符串转换成这个字符串,和替换为源字符串中找到的完全相同的数字 SomeOtherText_<2>r962<1>xyz SomeOtherText\u r962xyz 我猜这个表达式可能会这样做: import re regex = r"^.*?<(\d+)>_<(\d+)>.*

这是源字符串,它有两个通配符
,都是任意位数

SomeText<1>_<2>.value
SomeText\uu0.value
我需要将上面的字符串转换成这个字符串,
替换为源字符串中找到的完全相同的数字

SomeOtherText_<2>r962<1>xyz
SomeOtherText\u r962xyz

我猜这个表达式可能会这样做:

import re
regex = r"^.*?<(\d+)>_<(\d+)>.*$"
test_str = """

SomeText<192>_<283>.value
    SomeText<>_<>.value
    SomeOtherText_<192>r962<283>xyz
"""
subst = "SomeOtherText_<\\1>r962<\\2>xyz"

print(re.sub(regex, subst, test_str, 0, re.MULTILINE))
重新导入
regex=r“^.*?.*$”
测试_str=”“”
SomeText.值
SomeText.值
其他一些文本\u r962xyz
"""
subst=“SomeOtherText\u r962xyz”
打印(re.sub(regex,subst,test_str,0,re.MULTILINE))

如果您希望探索/简化/修改该表达式,请在的右上面板中对其进行说明。如果您愿意,您可以在中查看它与一些示例输入的匹配情况。

到目前为止您尝试了什么?请展示您的作品,以便我们帮助您修复。谢谢。它就像一个符咒。我只需要删除'>'和'