【问题标题】:python regular express extract certain words from a stringpython正则表达式从字符串中提取某些单词
【发布时间】:2019-10-07 15:28:36
【问题描述】:

我下面有一个长字符串,

'海拔信息:海拔范围:\xa05680 - 5720 ft\xa0\xa0\xa0\xa0(40 英尺闭合轮廓) NAVD88 高程:\xa05683 ft / 1732 m\xa0\xa0\xa0\xa0(范围 5683 - 5723 ft)纬度/经度 (WGS84)32° 56\' 17\'\' N, 116° 29\' 9\'\' W32.938182, -116.485858 (Dec Deg)548063 E 3644551 N, Zone 11 (UTM)CountryUnited 州州/省加利福尼亚(最高点) 县/二级地区三 Diego链接搜索引擎 - 在网络上搜索“Garnet Mountain”

我想通过正则表达式获取“美国”、“加利福尼亚(最高点)”、“圣地亚哥”等词。

我厌倦了通过下面的代码找到“美国”和“圣地亚哥”,但结果为空

country = re.findall(('Country\S([A-z]*)\SState'),table.text)
country
region = re.findall(('Region\S(.)\SLinks'),table.text)
region

我如何使用 re 来提取所有这些单词?

另外,如果 'United States' 被替换为 eg.'Japan'/'France'

country = re.findall(('Country\S([A-z]*)\SState'),table.text)
country

输出是“apan”/“rance”。为什么它们不是一个完整的单词。

谢谢!

【问题讨论】:

  • 请告诉我们您的 Python 脚本的编码是什么。
  • @Tim Biegeleisen 它应该是 utf8
  • 如果国家/地区中有空格 ([A-z]*) 并且它仅匹配区域的一个字符 (.),则该组(括号中的部分)将不匹配。 \S 匹配任何不是空白字符且您没有将其包含在组中的字符,因此第一个字符被剪切(最后一个不是因为它是“贪婪”匹配)。

标签: regex python-3.x beautifulsoup


【解决方案1】:

这对我有用:

import re

str = "'Elevation Info:Elevation range:\xa05680 - 5720 ft\xa0\xa0\xa0\xa0(40-foot closed contour) NAVD88 Elevation:\xa05683 ft / 1732 m\xa0\xa0\xa0\xa0(Range 5683 - 5723 ft)Latitude/Longitude (WGS84)32° 56\' 17\'\' N, 116° 29\' 9\'\' W32.938182, -116.485858 (Dec Deg)548063 E 3644551 N, Zone 11 (UTM)CountryUnited StatesState/ProvinceCalifornia (Highest Point) County/Second Level RegionSan DiegoLinksSearch Engines - search the web for \"Garnet Mountain'\""

country = re.findall(r"Country(\S*.*)State/", str)
province = re.findall(r"Province(\S*.*)County/", str)
city= re.findall(r"Region(\S*.*)Links", str)

print(country[0])
print(province[0])
print(city[0])
print("--------")

另外,您可以完全省略使用正则表达式,使用Split()

country = str.split("Country")[1].split("State/")[0]
province = str.split("Province")[1].split("County/")[0]
city = str.split("Region")[1].split("Links")[0]

【讨论】:

  • 感谢您的回答。但我还有一个问题,你能得到该省的“加利福尼亚(最高点)”吗?我只能在我的计算机中获取“加利福尼亚”。
  • @goxywood - 你使用我上面的代码和字符串吗?我在输出中得到了California (Highest Point),两种方法(regexsplit())。
  • 非常感谢!我现在可以得到整个字符串了!
【解决方案2】:

\S* 匹配零个或多个非空白字符。你在这里不需要那个。

改用.*(零个或多个非换行符)或明确说出您想要的字符。

例如[A-z0-9 ()]* 只允许使用字母、数字、空格和括号。

要在之前或之后去除空格,您可以在您的组外添加\s*(小s),并使用问号使您的* 不贪心:\s*(.*?)\s*

大家一起:

import re

str = "'Elevation Info:Elevation range:\xa05680 - 5720 ft\xa0\xa0\xa0\xa0(40-foot closed contour) NAVD88 Elevation:\xa05683 ft / 1732 m\xa0\xa0\xa0\xa0(Range 5683 - 5723 ft)Latitude/Longitude (WGS84)32° 56\' 17\'\' N, 116° 29\' 9\'\' W32.938182, -116.485858 (Dec Deg)548063 E 3644551 N, Zone 11 (UTM)CountryUnited StatesState/ProvinceCalifornia (Highest Point) County/Second Level RegionSan DiegoLinksSearch Engines - search the web for \"Garnet Mountain'\""

countries = re.findall(r"Country\s*(.*?)\s*State/", str)
provinces = re.findall(r"Province\s*(.*?)\s*County/", str)
regions = re.findall(r"Second Level Region\s*(.*?)\s*Links", str)

print("Countries:")
for country in countries:
  print(" ", country)

print("Provinces:")
for province in provinces:
  print(" ", province)

print("Second Level Regions:")
for region in regions:
  print(" ", region)

你可以在这里玩:https://regex101.com/r/yeiJVg/1

或者您可以将它们全部组合在一起以在更大的字符串中找到多个组:

import re

str = "'Elevation Info:Elevation range:\xa05680 - 5720 ft\xa0\xa0\xa0\xa0(40-foot closed contour) NAVD88 Elevation:\xa05683 ft / 1732 m\xa0\xa0\xa0\xa0(Range 5683 - 5723 ft)Latitude/Longitude (WGS84)32° 56\' 17\'\' N, 116° 29\' 9\'\' W32.938182, -116.485858 (Dec Deg)548063 E 3644551 N, Zone 11 (UTM)CountryUnited StatesState/ProvinceCalifornia (Highest Point) County/Second Level RegionSan DiegoLinksSearch Engines - search the web for \"Garnet Mountain'\""

matches = re.findall(r"Country\s*(.*?)\s*State/[.*\n]*Province\s*(.*?)\s*County/[.*\n]*Second Level Region\s*(.*?)\s*Links", str)

for match in matches:
    print("Country: {}, Province: {}, Second Level Region: {}".format(*match))

【讨论】:

    猜你喜欢
    • 2017-08-09
    • 2011-03-09
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    相关资源
    最近更新 更多