首页 > 解决方案 > 正则表达式 - 仅在数字之后和括号之前获取字符串(例如 1.Manhattan (NY))

问题描述

正则表达式的新手,到目前为止'([A-Za-z]^[^\(]+)' 我得到的是“列表索引超出范围”错误。

我只想从“1.Manhattan (NY)”中得到“Manhattan”

标签: pythonregex

解决方案


你可以使用这样的东西:

import re

txt = "(ex. 1. Manhattan (NY))"
m = re.search(r'[0-9]+\.\s+([^(]+)', txt)
if m:
    print(m.group(1))

这将打印出:

Manhattan

推荐阅读