Cap*_*Ron 1 python regex python-3.7
我的名字正则表达式在几个条目上已被证明是错误的:
find_name = re.search(r'^[^\d]*', clean_content)
Run Code Online (Sandbox Code Playgroud)
上面的代码会在几个条目上输出类似这样的内容:
TERRI BROWSING APT A # current output
Run Code Online (Sandbox Code Playgroud)
所以,我需要一种方法来消除它;它破坏了我程序的其余部分。我能想到的唯一标识符是我是否能以某种方式检测到第二个空格;并删除其后的所有字符。
我只需要名字和姓氏;IE
TERRI BROWSING # desired
Run Code Online (Sandbox Code Playgroud)
删除这些字符后,我可以.strip()删除尾随空格,只需要一种方法来删除第二个空格之后的所有字符......或者可能只检测到得到两个单词,仅此而已。
您甚至不需要正则表达式,因为您可以使用简单的拆分和连接:
text = 'TERRI BROWSING APT A'
' '.join(text.split(' ')[0:2])
Run Code Online (Sandbox Code Playgroud)
# 'TERRI BROWSING'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5213 次 |
| 最近记录: |