R - 从双字符串中提取城市名称

时间:2018-02-21 20:48:10

标签: r regex string gsub

我正在使用包含这样的城市名称的数据库:

cities <- c("Fairhope 3NE", "Gadsden 19N", "Selma 13 WNW", "Batesville 8 WNW", 
"Elgin 5 S", "Tucson 11 W", "Williams 35 NNW", "Fallbrook 5 NE", 
"Stovepipe Wells 1 SW", "Cortez 8 SE", "La Junta 17 WSW", "Montrose 11 ENE", 
"Everglades City 5 NE", "Sebring 23 SSE", "Brunswick 23 S", "Newton 11 SW", 
"Newton 8 W", "Watkinsville 5 SSE", "Des Moines 17 E", "Champaign 9 SW", 
"Shabbona 5 NNE", "Bedford 5 WNW", "Manhattan 6 SSW", "Oakley 19 SSW", 
"Bowling Green 21 NNE", "Versailles 3 NNW", "Lafayette 13 SE", 
"Monroe 26 N", "Goodridge 12 NNW", "Chillicothe 22 ENE", "Joplin 24 N", 
"Salem 10 W", "Holly Springs 4 N", "Newton 5 ENE", "Asheville 13 S", 
"Asheville 8 SSW", "Durham 11 W", "Jamestown 38 WSW", "Medora 7 E", 
"Northgate 5 ESE", "Harrison 20 SSE", "Lincoln 11 SW", "Lincoln 8 ENE", 
"Whitman 5 ENE", "Las Cruces 20 N", "Los Alamos 13 W", "Socorro 20 N", 
"Mercury 3 SSW", "Coshocton 8 NNE", "Goodwell 2 E", "Stillwater 2 W", 
"Stillwater 5 WNW", "Coos Bay 8 SW", "Corvallis 10 SSW", "Riley 10 WSW", 
"Blackville 3 W", "McClellanville 7 NE", "Aberdeen 35 WNW", "Buffalo 13 ESE", 
"Pierre 24 S", "Sioux Falls 14 NNE", "Crossville 7 NW", "Austin 33 NW", 
"Bronte 11 NNE", "Edinburg 17 NNE", "Monahans 6 ENE", "Muleshoe 19 S", 
"Palestine 6 WNW", "Panther Junction 2 N", "Necedah 5 WNW")

我想只提取城市名称。以下代码适用于某些情况:

gsub( " .*$", "", cities)

但对于具有双字名称的城市而言失败,例如Stovepipe Wells 1 SWLa Junta 17 WSW

针对这些案例的任何解决方案的想法?

5 个答案:

答案 0 :(得分:3)

您可以删除以数字开头的所有子字符串:

ForEach-Object

下面,

  • > sub("\\s*\\d.*", "", cities) [1] "Fairhope" "Gadsden" "Selma" "Batesville" "Elgin" "Tucson" "Williams" "Fallbrook" "Stovepipe Wells" [10] "Cortez" "La Junta" "Montrose" "Everglades City" "Sebring" "Brunswick" "Newton" "Newton" "Watkinsville" [19] "Des Moines" "Champaign" "Shabbona" "Bedford" "Manhattan" "Oakley" "Bowling Green" "Versailles" "Lafayette" [28] "Monroe" "Goodridge" "Chillicothe" "Joplin" "Salem" "Holly Springs" "Newton" "Asheville" "Asheville" [37] "Durham" "Jamestown" "Medora" "Northgate" "Harrison" "Lincoln" "Lincoln" "Whitman" "Las Cruces" [46] "Los Alamos" "Socorro" "Mercury" "Coshocton" "Goodwell" "Stillwater" "Stillwater" "Coos Bay" "Corvallis" [55] "Riley" "Blackville" "McClellanville" "Aberdeen" "Buffalo" "Pierre" "Sioux Falls" "Crossville" "Austin" [64] "Bronte" "Edinburg" "Monahans" "Muleshoe" "Palestine" "Panther Junction" "Necedah" > - 匹配0+空格
  • \\s* - 数字
  • \\d - 字符串的其余部分。

请参阅regex demo.*仅执行单个匹配并替换操作。

答案 1 :(得分:2)

您可以使用

gsub("(\\D+)\\s+.*", "\\1", cities)

产生

 [1] "Fairhope"         "Gadsden"          "Selma"            "Batesville"       "Elgin"           
 [6] "Tucson"           "Williams"         "Fallbrook"        "Stovepipe Wells"  "Cortez"          
[11] "La Junta"         "Montrose"         "Everglades City"  "Sebring"          "Brunswick"       
[16] "Newton"           "Newton"           "Watkinsville"     "Des Moines"       "Champaign"       
[21] "Shabbona"         "Bedford"          "Manhattan"        "Oakley"           "Bowling Green"   
[26] "Versailles"       "Lafayette"        "Monroe"           "Goodridge"        "Chillicothe"     
[31] "Joplin"           "Salem"            "Holly Springs"    "Newton"           "Asheville"       
[36] "Asheville"        "Durham"           "Jamestown"        "Medora"           "Northgate"       
[41] "Harrison"         "Lincoln"          "Lincoln"          "Whitman"          "Las Cruces"      
[46] "Los Alamos"       "Socorro"          "Mercury"          "Coshocton"        "Goodwell"        
[51] "Stillwater"       "Stillwater"       "Coos Bay"         "Corvallis"        "Riley"           
[56] "Blackville"       "McClellanville"   "Aberdeen"         "Buffalo"          "Pierre"          
[61] "Sioux Falls"      "Crossville"       "Austin"           "Bronte"           "Edinburg"        
[66] "Monahans"         "Muleshoe"         "Palestine"        "Panther Junction" "Necedah"        

<小时/> 解释,这说:

(\\D+) # not a digit, 1+ times
\\s+   # at least one whitespace
.*     # rest of the string

然后在第一个捕获的组中替换它,\\1

答案 2 :(得分:2)

假设每个字符串以相同的模式结束:

  • 空间
  • 位(S)
  • 可选空间,
  • ENSW
  • 的组合

See code in use here

gsub(" \\d+ ?[ENSW]+$", "", cities)

结果:

 [1] "Fairhope"         "Gadsden"          "Selma"            "Batesville"      
 [5] "Elgin"            "Tucson"           "Williams"         "Fallbrook"       
 [9] "Stovepipe Wells"  "Cortez"           "La Junta"         "Montrose"        
[13] "Everglades City"  "Sebring"          "Brunswick"        "Newton"          
[17] "Newton"           "Watkinsville"     "Des Moines"       "Champaign"       
[21] "Shabbona"         "Bedford"          "Manhattan"        "Oakley"          
[25] "Bowling Green"    "Versailles"       "Lafayette"        "Monroe"          
[29] "Goodridge"        "Chillicothe"      "Joplin"           "Salem"           
[33] "Holly Springs"    "Newton"           "Asheville"        "Asheville"       
[37] "Durham"           "Jamestown"        "Medora"           "Northgate"       
[41] "Harrison"         "Lincoln"          "Lincoln"          "Whitman"         
[45] "Las Cruces"       "Los Alamos"       "Socorro"          "Mercury"         
[49] "Coshocton"        "Goodwell"         "Stillwater"       "Stillwater"      
[53] "Coos Bay"         "Corvallis"        "Riley"            "Blackville"      
[57] "McClellanville"   "Aberdeen"         "Buffalo"          "Pierre"          
[61] "Sioux Falls"      "Crossville"       "Austin"           "Bronte"          
[65] "Edinburg"         "Monahans"         "Muleshoe"         "Palestine"       
[69] "Panther Junction" "Necedah"   

答案 3 :(得分:1)

这应该有效:

gsub( " [1-9].*$", "", cities)

答案 4 :(得分:0)

此正则表达式应该可以在任何地方使用,包括R:.+?(?=\ \d)

它包括第一个空格后跟数字的所有内容。