How to extract locations from strings of addresses?

조회 수: 1 (최근 30일)
pietro
pietro 2019년 1월 10일
편집: Stephen23 2019년 1월 13일
Hi all,
I have more than a thousand of strings containing addresses. How can I programmatically extract the locations, like cities (e.g. Rome, London, etc) and countries (e.g. Italy, UK, etc)?
Thanks
Pietro
  댓글 수: 8
Akira Agata
Akira Agata 2019년 1월 13일
편집: Akira Agata 2019년 1월 13일
How abou the following approach?
  1. Split each line with ';' character into 1 or 2 segments
  2. Split each segment(s) with ',' mark into several fragments
  3. Extract the last two fragments
The following is my try:
str = [...
"Sustainable Industrial Systems, School of Chemical Engineering and Analytical Science, The University of Manchester, Manchester, United Kingdom; Dipartimento di Scienze Agrarie e Ambientali - Produzione, Territorio, Agroenergia, Università degli Studi di Milano, Milan, Italy";
"Department of Agricultural and Environmental Sciences - Production, Landscape, Agroenergy, Università degli Studi di Milano, Via G. Celoria 2, Milano, Italy; Department of Earth and Environmental Sciences, University of Milano-Bicocca, Piazza della Scienza 1, Milano, Italy";
"Department of Land and Agriculture and Forestry Systems, University of Padua, Viale dell'Università 16, 35020 Legnaro (PD), Italy";
"Dipartimento di Agraria, University of Sassari, Sassari, Italy"];
for kk1 = 1:numel(str)
strSplit = strsplit(str(kk1),';');
strOut = repelem("",1,numel(strSplit));
for kk2 = 1:numel(strSplit)
strTmp = strsplit(strSplit(kk2),', ');
strOut(kk2) = strjoin(strTmp(end-1:end),', ');
end
disp(strjoin(strOut,'; '))
end
The output is as follows:
Manchester, United Kingdom; Milan, Italy
Milano, Italy; Milano, Italy
35020 Legnaro (PD), Italy
Sassari, Italy
pietro
pietro 2019년 1월 13일
Hi Akira,
thank you for the answer. Well, it is a good solution. I think I might adopt it.
Cheers,
Michele

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2019년 1월 13일
편집: Stephen23 2019년 1월 13일
Using one simple regular expression:
>> C = {...
'Sustainable Industrial Systems, School of Chemical Engineering and Analytical Science, The University of Manchester, Manchester, United Kingdom; Dipartimento di Scienze Agrarie e Ambientali - Produzione, Territorio, Agroenergia, Università degli Studi di Milano, Milan, Italy';
'Department of Agricultural and Environmental Sciences - Production, Landscape, Agroenergy, Università degli Studi di Milano, Via G. Celoria 2, Milano, Italy; Department of Earth and Environmental Sciences, University of Milano-Bicocca, Piazza della Scienza 1, Milano, Italy';
'Department of Land and Agriculture and Forestry Systems, University of Padua, Viale dell''Università 16, 35020 Legnaro (PD), Italy';
'Dipartimento di Agraria, University of Sassari, Sassari, Italy'};
>> M = regexp(C,'[^,]+,[^,]+(?=;|$)','match');
>> M{:}
ans =
' Manchester, United Kingdom'
' Milan, Italy'
ans =
' Milano, Italy'
' Milano, Italy'
ans =
' 35020 Legnaro (PD), Italy'
ans =
' Sassari, Italy'

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Statics and Dynamics에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by