英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论