gpt4 book ai didi

Python re.sub 和 re.match 不匹配?

转载 作者:太空宇宙 更新时间:2023-11-03 17:40:46 24 4
gpt4 key购买 nike

我正在尝试从文件中删除以下字符串的所有实例:

{ "userID":(some 6 digit number), "array":[]},

特别是,我想找到所有这样的子字符串并将它们替换为空('')

我首先使用 re.match 来确保我的表达是正确的:

matchObj = re.match( r'({.*?"array":\[\]\},?)', g)

这工作正常并返回我想要的内容(我将问号放入两次以关闭 re 的贪婪默认值)。但是当我转向 re.sub 时,它匹配了字符串的许多部分,我没想到它会匹配。特别是这个表达式:

matchObj = re.match( r'({.*?"array":\[\]\},?)', g)
ggg = re.sub( r'({.*?"array":\[\]\},?)', '', g)

g 的值为:

g = 'fedsgedsgs {"all": [{"userID": 777, "array":[]},azgagaga{"userID": 777, "array":[{"expand":"abs","id":503711372,"sport":18,"start_time":"2015-04-15T16:11:12.000Z","local_start_time":"2015-04-15T17:11:12.000Z","distance":4.281959056854248,"duration":2\
891.0,"speed_avg":5.332083225415182,"speed_max":6.74372,"altitude_min":27.0,"altitude_max":61.0,"ascent":80.0,"descent":86.0},{"expand":"abs","id":470811412,"sport":18,"start_time":"2015-02-11T09:27:10.000Z","local_start_time":"2015-02-\
11T10:27:10.000Z","distance":0.0,"duration":0.0},{"expand":"abs","id":470755226,"sport":18,"start_time":"2015-02-11T09:25:04.000Z","local_start_time":"2015-02-11T10:25:04.000Z","distance":0.0,"duration":0.0,"speed_max":0.0,"altitude_min\
":45.0,"altitude_max":45.0},{"expand":"abs","id":470749841,"sport":18,"start_time":"2015-02-11T09:10:43.000Z","local_start_time":"2015-02-11T10:10:43.000Z","distance":0.7858999967575073,"duration":479.0,"speed_avg":5.90655529922135,"spe\
ed_max":6.82629,"altitude_min":35.0,"altitude_max":57.0,"ascent":45.0,"descent":32.0}]},{"userID": 777, "array":[{"expand":"abs","id":470745921,"sport":0,"start_time":"2015-02-11T09:00:48.000Z","local_start_time":"2015-02-11T15:00:48.00\
0Z","distance":0.0,"duration":15.0,"speed_avg":0.0}]},{"userID": 777, "array":[{"expand":"abs","id":498050248,"sport":2,"start_time":"2015-04-06T14:00:03.000Z","local_start_time":"2015-04-06T19:00:03.000Z","distance":16.55500030517578,"\
duration":2793.51,"speed_avg":21.334450601083514,"speed_max":36.3397,"altitude_min":1.8,"altitude_max":35.5,"ascent":50.7,"descent":61.8},{"expand":"abs","id":498049916,"sport":2,"start_time":"2015-04-06T13:59:35.000Z","local_start_time\
":"2015-04-06T18:59:35.000Z","distance":0.010999999940395355,"duration":10.2,"speed_avg":3.882352920139537,"speed_max":2.072,"altitude_min":8.4,"altitude_max":8.4,"ascent":0.0,"descent":0.0},{"expand":"abs","id":486139822,"sport":2,"sta\
rt_time":"2015-03-15T00:21:08.000Z","local_start_time":"2015-03-15T06:21:08.000Z","distance":23.302000045776367,"duration":3997.54,"speed_avg":20.984705635164357,"speed_max":38.4344,"altitude_min":-7.3,"altitude_max":14.6,"ascent":20.1,\
"descent":42.1},{"expand":"abs","id":486139782,"sport":2,"start_time":"2015-03-15T00:20:50.000Z","local_start_time":"2015-03-15T06:20:50.000Z","distance":0.0,"duration":2.99,"speed_avg":0.0,"speed_max":0.0,"altitude_min":4.8,"altitude_m\
ax":4.8,"ascent":0.0,"descent":0 {"userID": 777, "array":[]}, mmmmmmmm {"userID": 7767, "array":[]}, gggggggg {"userID": 74577, "array":[]}, ggggggggggggggg {"userID": 774447, "array":[]}, hrdshe {"userID": 722277, "array":[]},'

导致 ggg 的输出:

In[37]:   ggg
Out[37]: 'fedsgedsgs azgagaga mmmmmmmm gggggggg ggggggggggggggg hrdshe '

表达式正在用 '' 替换这种形式的表达式:

  { "userID":(some 6 digit number), "array":[lots of json objects printed here.....]},

而我想保留这些表达式(具有非空数组的表达式)不变。

我尝试从 \[\] 中删除转义键,因为我只想匹配“[]”,但随后收到一条错误消息,指出我的输入不完整表达。为什么我将 [....stuff....] 与里面的垃圾匹配,如何只匹配“[]”?

更新

所以这是有效的:

ggg = re.sub( r'"userID": [0-9]{6,6}, "array":[]},', '找到它', g)

不知怎的,贪婪似乎不是一个问题。如果有人可以向我解释为什么上述方法有效但原始尝试无效,我真的很想知道。

最佳答案

re.match() 是隐式锚定的。也就是说:

re.match('foo', content)   # find foo only at the beginning of content

...与...相同

re.match('^foo', content)  # find foo only at the beginning of content

...而:

re.sub('foo', 'bar', content) # replace foo with bar everywhere in content

...隐式未锚定,使其行为与

相同
re.search('foo', content) # find foo everywhere in content

...它将在 content各处找到 foo 的实例,而不仅仅是在开头。

<小时/>

因此,要使您使用 re.sub() 的正则表达式的行为与使用 re.match() 的方式相同,请添加显式 ^ anchor 。

<小时/>

(顺便说一句 - 尝试以这种方式修改 JSON 注定会以痛苦和磨难结束。解析、更新和重新序列化 - 否则你将面临各种不必要的错误。

关于Python re.sub 和 re.match 不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30584338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com