? list.sort_by { |s| s[/(?<=\d\d-\d\d\d\s).*?(?=ul.)/] } #? [ # [0] "Urz?d Skarbowy 59-700 Boles?awiec ul. Garncarska 10", # [1] "Urz?d Skarbowy 57-500 Bystrzyca K?odzka ul. Adama Mickiewicza 5", # [2] "Urz?d Skarbowy 58-200 Dzier?oniów ul. Pocztowa 14", # [3] "Urz?d Skarbowy 67-200 G?ogów ul. Adama Mickiewicza 53" # ]
请注意,上面的正则表达式假设您具有ul.
街道名称终止符.可能在大多数情况下,使用街道名称中的第一个单词(或者发明更复杂的终结符)是安全的.
? list.sort_by { |s| s[/(?<=\d{2}-\d{3}\s)\w+/] }
UPD联合收割机:
? list.sort_by do |s| ? s[/(?<=\d\d-\d\d\d\s).*?(?=ul.)/] || \ # try to catch `ul.` ? s[/(?<=\d{2}-\d{3}\s)\p{Alpha}+/] || \ # no worries, use first word ? '' # OK, failed to catch anything ? end
请注意\p{Alpha}
匹配器,\w
因变音符号而无法在这里工作.
NB我想的越多,我就越相信如果没有ul.
被捕获,它应该按匹配排序直到字符串的结尾:
? list.sort_by do |s| ? (s[/(?<=\d\d-\d\d\d\s).*?(?=ul.)/] || s[/(?<=\d{2}-\d{3}\s).+?$/]).strip ? end