简体   繁体   English

正则表达式替换无法用正则表达式变量替换 Python

[英]regex replace unable to substitute in Python with regex variables

we have huge number of files where we need to transfrom to json here is the sampple data of one file我们有大量文件需要转换为 json 这是一个文件的示例数据

{
1=2,
4=tt,
6=9
}
{
1=gg,
2=bd,
6=bb
}

I am using python to convert the data where regex expression is working fine but the same regex is not working when i implementing in python code here is the code我正在使用 python 转换正则表达式工作正常的数据,但是当我在 python 代码中实现时,相同的正则表达式不起作用

import numpy as np
f = open('/Users/rahulvarma/Downloads/2020120911.txt', 'r')
content = f.read()
import re
regex = r"([0-9]+)(=)((.*)+)"
subst = "\"$1\":\"$3\","
result = re.sub(regex, subst, content,  0, re.MULTILINE)

if result:
    print (result)

but my were但我是

{
"$1":"$3",
"$1":"$3",
"$1":"$3"
}
{
"$1":"$3",
"$1":"$3",
"$1":"$3"
}

my expected output should be我预期的 output 应该是

{
"1":"2",
"4":"tt",
"6":"9"
}
{
"1":"gg",
"2":"bd",
"6":"bb"
}

You can search using this regex:您可以使用此正则表达式进行搜索:

(\d+)=([^,\n]*)(,|$)

And replace using:并替换使用:

"\1":"\2"\3

RegEx Demo正则表达式演示

Code:代码:

regex = r"(\d+)=([^,\n]*)(,|$)"

result = re.sub(regex, r'"\1":"\2"\3', input_str, 0, re.MULTILINE)

RegEx Details:正则表达式详细信息:

  • (\d+) : Match 1+ digits in captured group #1 (\d+) :匹配捕获组#1 中的 1+ 个数字
  • = : Match = character = : 匹配=字符
  • ([^,\n]*) : Match 0 or more of any characters that are not , and not \n in captured group #2 ([^,\n]*) : 匹配 0 个或多个不是,而不是\n在捕获组 #2 中的任何字符
  • (,|$) : Match comma or end of line in captured group #3 (,|$) : 匹配捕获组 #3 中的逗号或行尾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM