In [0]:
import re
In [2]:
"""
Suppose we want to extract location (such as 北京) from the sentence
"""

inp = "查 一 下 去 北 京 方 向 交 通 情 况 怎 么 样"
rule = r"(?:向|往|进|朝|去) (.*) (?:方 向)"
print(re.findall(rule, inp))
['北 京']
In [3]:
"""
Suppose now we want to extract two address names at the same time
"""

inp = "给 我 导航 到 <ADDRESS_NM> 上海市闸北区场中路270弄 </ADDRESS_NM> <ADDRESS_NM> 幸福新苑小区 </ADDRESS_NM> 的路线"
rule = r'<.*?> (.*?) </.*?>'
print(re.findall(rule, inp))
['上海市闸北区场中路270弄', '幸福新苑小区']