我正在使用一条规则使用正则表达式在python中获取子字符串。但是我发现有些结果会有领先的空白,而有些却没有。我知道我可以使用.strip()删除空格。但我想了解为什么存在空白。有人可以帮忙吗?
谢谢
示例1.(无前导空格)。
import re
utterance = 'can i make a call to +21231313'
re.findall('[-|#|+]*[0-9]*[-\s]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*', utterance.strip())
结果:
['+21231313']
示例2。(前导空白)。
import re
utterance = 'can i make a call to -21231313'
re.findall('[-|#|+]*[0-9]*[-\s]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*', utterance.strip())
结果:
[' -21231313']
示例3。(前导空白)。
import re
utterance = 'can i make a call to 21231313'
re.findall('[-|#|+]*[0-9]*[-\s]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*', utterance.strip())
结果:
[' 21231313']