英文:
Why this regex is not matching the text string?
问题
以下是您的代码的翻译部分:
import re
string = " S/O: fathersName,other details,some other details."
fathersName = re.match(r":.*?,", string).group(0)
正则表达式匹配应该匹配字符串中的fathersName
部分,但我收到一个属性错误,表示未找到匹配项。
我甚至尝试使用re.match(':', string)
,但仍然找不到匹配项。
我认为这与冒号符号有关,但我不确定。
我正在使用Jupyter Notebook。
英文:
I have a python code as follows:
import re
string=" S/O: fathersName,other details,some other details."`
fathersName=re.match(r":.*?,",string).group(0)
The regex match is supposed to match the fatherName
part of the string, but I get a attributr error saying no mathces found
I even tried with re.match(':',string)
and still I get 0 matches.
I think it is somehow related to : symbol, but I'm not sure.
I am using Jupyter Notebook
答案1
得分: -2
fathersName=re.search(r"S/O:\s*(.*?),",string).group(1)
英文:
fathersName=re.search(r"S/O:\s*(.*?),",string).group(1)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论