簡體   English   中英

字符串包含單詞的兩個字符之間的正則表達式

[英]Regex between two characters where string contains a word

我希望從以各種格式編寫的附屬機構中提取父大學名稱。 例如:

institute of organic chemistry, rwth aachen university, landoltweg 1, 52074 aachen, germany
school of medical sciences, university of new south wales, save sight institute, university of sydney
save sight institute, university of sydney
unit for laboratory animal medicine, university of michigan, ann arbor 48109
membrane dynamics, department of biology, technische universität darmstadt, schnittspahnstrasse 3, 64287 darmstadt, germany 
university of new south wales, sydney, australia

我的想法通常是父大學通常夾在兩個逗號之間,並且包含“大學”(或“大學”和其他語言)這個詞。 所以我的正則表達式如下:

(?:,)((.*?university.*?)|(.*?universität.*?))(?:,|$)

但是,我在以下兩個地方被絆倒了:

  1. 如果包含“大學”的組不是第二個逗號三明治(例如,第 5 行)
  2. 如果包含“大學”的組位於完整字符串的開頭(例如,第 6 行)

也對如何提取它的其他想法持開放態度。 我考慮過對地址進行地理編碼,然后在 Google 上進行反向地理編碼以找到該地點。 但是,我有數百萬條記錄。

這個答案讓我很接近。

我做了類似的事情,我有兩個筆記:

  1. 谷歌會比你更擅長解析信息,所以你可以把它交給谷歌地理定位。
  2. 至少可以說,這並不是正則表達式擅長的事情。

我已經采取了您上面的文字並執行了以下操作來展示一個示例(對不起,它在 python 中,我不知道 r):

BASE_URL = 'https://maps.googleapis.com/maps/api/geocode/json?key=APIKEY&address='
for line in s.split('\n'):
    requests.get(BASE_URL+line).json()['results'][0]['address_components']

[{u'long_name': u'1', u'types': [u'street_number'], u'short_name': u'1'}, {u'long_name': u'Landoltweg', u'types': [u'route'], u'short_name': u'Landoltweg'}, {u'long_name': u'Aachen', u'types': [u'locality', u'political'], u'short_name': u'AC'}, {u'long_name': u'K\xf6ln', u'types': [u'administrative_area_level_2', u'political'], u'short_name': u'K'}, {u'long_name': u'Nordrhein-Westfalen', u'types': [u'administrative_area_level_1', u'political'], u'short_name': u'NRW'}, {u'long_name': u'Germany', u'types': [u'country', u'political'], u'short_name': u'DE'}, {u'long_name': u'52074', u'types': [u'postal_code'], u'short_name': u'52074'}]
[{u'long_name': u'8', u'types': [u'street_number'], u'short_name': u'8'}, {u'long_name': u'Macquarie Street', u'types': [u'route'], u'short_name': u'Macquarie St'}, {u'long_name': u'Sydney', u'types': [u'locality', u'political'], u'short_name': u'Sydney'}, {u'long_name': u'Council of the City of Sydney', u'types': [u'administrative_area_level_2', u'political'], u'short_name': u'Sydney'}, {u'long_name': u'New South Wales', u'types': [u'administrative_area_level_1', u'political'], u'short_name': u'NSW'}, {u'long_name': u'Australia', u'types': [u'country', u'political'], u'short_name': u'AU'}, {u'long_name': u'2000', u'types': [u'postal_code'], u'short_name': u'2000'}]
[{u'long_name': u'8', u'types': [u'street_number'], u'short_name': u'8'}, {u'long_name': u'Macquarie Street', u'types': [u'route'], u'short_name': u'Macquarie St'}, {u'long_name': u'Sydney', u'types': [u'locality', u'political'], u'short_name': u'Sydney'}, {u'long_name': u'Council of the City of Sydney', u'types': [u'administrative_area_level_2', u'political'], u'short_name': u'Sydney'}, {u'long_name': u'New South Wales', u'types': [u'administrative_area_level_1', u'political'], u'short_name': u'NSW'}, {u'long_name': u'Australia', u'types': [u'country', u'political'], u'short_name': u'AU'}, {u'long_name': u'2000', u'types': [u'postal_code'], u'short_name': u'2000'}]
[{u'long_name': u'2800', u'types': [u'street_number'], u'short_name': u'2800'}, {u'long_name': u'Plymouth Road', u'types': [u'route'], u'short_name': u'Plymouth Rd'}, {u'long_name': u'Northside', u'types': [u'neighborhood', u'political'], u'short_name': u'Northside'}, {u'long_name': u'Ann Arbor', u'types': [u'locality', u'political'], u'short_name': u'Ann Arbor'}, {u'long_name': u'Washtenaw County', u'types': [u'administrative_area_level_2', u'political'], u'short_name': u'Washtenaw County'}, {u'long_name': u'Michigan', u'types': [u'administrative_area_level_1', u'political'], u'short_name': u'MI'}, {u'long_name': u'United States', u'types': [u'country', u'political'], u'short_name': u'US'}, {u'long_name': u'48109', u'types': [u'postal_code'], u'short_name': u'48109'}, {u'long_name': u'2800', u'types': [u'postal_code_suffix'], u'short_name': u'2800'}]
[{u'long_name': u'3', u'types': [u'street_number'], u'short_name': u'3'}, {u'long_name': u'Schnittspahnstra\xdfe', u'types': [u'route'], u'short_name': u'Schnittspahnstra\xdfe'}, {u'long_name': u'Darmstadt-Ost', u'types': [u'political', u'sublocality', u'sublocality_level_1'], u'short_name': u'Darmstadt-Ost'}, {u'long_name': u'Darmstadt', u'types': [u'locality', u'political'], u'short_name': u'Darmstadt'}, {u'long_name': u'Darmstadt', u'types': [u'administrative_area_level_2', u'political'], u'short_name': u'DA'}, {u'long_name': u'Hessen', u'types': [u'administrative_area_level_1', u'political'], u'short_name': u'HE'}, {u'long_name': u'Germany', u'types': [u'country', u'political'], u'short_name': u'DE'}, {u'long_name': u'64287', u'types': [u'postal_code'], u'short_name': u'64287'}]
[{u'long_name': u'Sydney', u'types': [u'locality', u'political'], u'short_name': u'Sydney'}, {u'long_name': u'Randwick City Council', u'types': [u'administrative_area_level_2', u'political'], u'short_name': u'Randwick'}, {u'long_name': u'New South Wales', u'types': [u'administrative_area_level_1', u'political'], u'short_name': u'NSW'}, {u'long_name': u'Australia', u'types': [u'country', u'political'], u'short_name': u'AU'}, {u'long_name': u'2052', u'types': [u'postal_code'], u'short_name': u'2052'}]

話雖如此,我無法想象您將有 2M 條唯一記錄要傳遞,因此您可能希望在發送它們之前將它們分組(或者甚至使用 zip 代碼作為密鑰?)。

避免逗號查找周圍的文本可能更容易:

([^,]*(university|universität)[^,]*)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM