Why do these two regular expressions work differently with re.sub(), but return the same match with re.search()?

huangapple go评论70阅读模式
英文:

Why do these two regular expressions work differently with re.sub(), but return the same match with re.search()?

问题

Both regex are fetching same match but why does ",([\d-]+)" work as expected and not the "(,\d*-?\d*-?\d*)"? I was expecting both regex to give me same output during re.sub(). What am I missing?

>>> print(re.search(r",([\d-]+)", "Sabrina Green,802-867-5309,System Administrator"))       
<re.Match object; span=(13, 26), match=',802-867-5309'>
>>> print(re.search(r"(,\d*-?\d*-?\d*)", "Sabrina Green,802-867-5309,System Administrator"))       
<re.Match object; span=(13, 26), match=',802-867-5309'>
>>> 

>>> print(re.sub(r",([\d-]+)", r",+1-\1", "Sabrina Green,802-867-5309,System Administrator"))      
Sabrina Green,+1-802-867-5309,System Administrator
>>> print(re.sub(r"(,\d*-?\d*-?\d*)", r",+1-\1", "Sabrina Green,802-867-5309,System Administrator"))
Sabrina Green,+1-,802-867-5309,+1-,System Administrator
>>> 
英文:

Both regex are fetching same match but why does ",([\d-]+)" work as expected and not the "(,\d*-?\d*-?\d*)"? I was expecting both regex to give me same output during re.sub(). What am I missing?

>>> print(re.search(r",([\d-]+)", "Sabrina Green,802-867-5309,System Administrator"))       
<re.Match object; span=(13, 26), match=',802-867-5309'>
>>> print(re.search(r"(,\d*-?\d*-?\d*)", "Sabrina Green,802-867-5309,System Administrator"))       
<re.Match object; span=(13, 26), match=',802-867-5309'>
>>> 
>>> print(re.sub(r",([\d-]+)", r",+1-\1", "Sabrina Green,802-867-5309,System Administrator"))      
Sabrina Green,+1-802-867-5309,System Administrator
>>> print(re.sub(r"(,\d*-?\d*-?\d*)", r",+1-\1", "Sabrina Green,802-867-5309,System Administrator"))
Sabrina Green,+1-,802-867-5309,+1-,System Administrator
>>> 

Expected output: Sabrina Green,+1-802-867-5309,System Administrator

答案1

得分: 0

你的正则表达式字符串中有拼写错误。
首先,你的逗号在括号内,所以应该是 ,+1-,802-867-5309

其次,你应该用 + 替换 * 在你的正则表达式中。注意 5309,+1-,System Administrator

将来如果你在使用正则表达式时遇到问题,你可以查看这个网站。它会分解正则表达式并为你提供正则表达式的可视化表示。

英文:

You have a typo in your regex string.
First, your comma is inside the parethesis, thus ,+1-,802-867-5309

Second, you should replace the * with + in your regex. Notice the 5309,+1-,System Administrator

In the future if you're having trouble with regex. You can check out this site. It will break down the regex and give you a visual representation of what you're regex is doing.

huangapple
  • 本文由 发表于 2023年3月12日 06:43:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定