python - 正则表达式 - 仅在数字之后和括号之前获取字符串(例如 1.Manhattan (NY))
问题描述
正则表达式的新手,到目前为止'([A-Za-z]^[^\(]+)'
我得到的是“列表索引超出范围”错误。
我只想从“1.Manhattan (NY)”中得到“Manhattan”
解决方案
你可以使用这样的东西:
import re
txt = "(ex. 1. Manhattan (NY))"
m = re.search(r'[0-9]+\.\s+([^(]+)', txt)
if m:
print(m.group(1))
这将打印出:
Manhattan
推荐阅读
- delphi - Delphi 使用 LiveBindings 时无法更改此属性
- java - Java中的超时/驱逐列表
- ruby - 如何使用 OptionParser 将空字符串传递到命令行
- google-cloud-platform - 谷歌SQL云导入数据库问题
- mysql - 重新导入数据后mysql查询不执行,之前工作正常
- mysql - MySQL按外键分区
- mysql - Knex 迁移:事务查询已完成
- python - Django 错误没有这样的表:main.auth_user__old
- c++ - PCL 出错:架构 x86_64 @pcl 的未定义符号
- python - numba @vectorize target='parallel' TypeError