How to extract the correct data with regexp?

2 ビュー (過去 30 日間)
Linus Dock
Linus Dock 2016 年 10 月 19 日
回答済み: VBBV 2023 年 3 月 3 日
Hello again! This is a continuation of a previous question.
I'm using this expression to extract data from a cell of strings:
tokens = regexp(DATALow, '\<(R\d{2}[A-Z]?)/.*?(\d{4})[A-Z]*(?:(?:\d{4})[A-Z])?\>', 'tokens');
It's working quite well but I'm getting some unwanted extra data from my strings. The problematic strings look like this:
'METAR ESGJ 072350Z 16003KT 1500 BR VV002 M03/M03 Q1009 R19/790042'
and
'METAR ESSA 181550Z 13007KT 1100 R01L/P1500N R19R/1400N R01R/1400N R19L/P1500N SN VV007 M01/M01 Q1013 R01L/590249 R08/590247 R01R/12//70 BECMG 9999 NSW BKN012'
I would like to change my regexp to not extract the '0042' from the first string and '9999' from the second. How do I modify the expression without losing the wanted data?
The data I would like to collect is '1500' from the Group 'R01L/P1500N' and '1400' from 'R19R/1400N' and so on in the second string.
This is a sample of DATALow:
'METAR ESOE 180920Z 08006KT 1400 R01/1200VP2000N R19/1700N -SN BKN004 M00/M01 Q1013'
'METAR ESOK 180450Z AUTO 07006KT 1200 R03/P1500N R21/P1500N -SN FEW006/// SCT008/// M02/M03 Q1014'
'METAR ESOK 180520Z AUTO 07006KT 1400 R03/P1500N R21/P1500N -SN FEW006/// M02/M03 Q1014'
'METAR ESOW 180550Z 09005KT 1200 R01/P1500D R19/P1500N SN VV010 M01/M02 Q1016 R01/0///95'
'METAR ESOW 180650Z 09005KT 0800 R01/1300D R19/P1500D SN VV005 M01/M02 Q1016 R01/490232'
'METAR ESOW 180720Z 09005KT 1200 R01/P1500U R19/P1500U SN VV010 M01/M02 Q1016 R01/490232'
'METAR ESOW 181020Z 11006KT 1100 R01/1300N R19/1300N -SN VV010 M01/M01 Q1014 R01/490232'
'METAR ESOW 181050Z 11007KT 1100 R01/1300U R19/1200U -SN VV010 M01/M01 Q1014 R01/490232'
'METAR ESOW 181120Z 11006KT 1100 R01/1200D R19/1200D SN VV010 M01/M01 Q1014 R01/490232'
'METAR ESOW 181150Z 10007KT 1000 R01/1100N R19/1100N SN VV005 M01/M01 Q1013 R01/490232'
'METAR ESOW 181220Z 11007KT 1100 R01/P1500N R19/P1500N -SN VV008 M01/M01 Q1013 R19/690138'
'METAR ESOW 181250Z 11009KT 1200 R01/1300U R19/1300N SN VV009 M01/M01 Q1013 R19/690138'
'METAR ESSA 181320Z 15010KT 1500 -SN VV010 M00/M02 Q1013 R01L/12//66 R08/12//74 R01R/12//70 BECMG 2500 SN BKN014'
'METAR ESSA 181350Z 14008KT 1500 R01L/P1500N R19R/P1500N R01R/1500N R19L/P1500N SN VV013 M01/M01 Q1013 R01L/12//66 R08/12//74 R01R/12//70 TEMPO 4000 VV015'
'METAR ESSA 181350Z 14008KT 1500 R01L/P1500N R19R/P1500N R01R/1500N R19L/P1500N -SN VV013 M01/M01 Q1013 R01L/12//66 R08/12//74 R01R/12//70 TEMPO 4000 VV015'
'METAR ESSA 181420Z 14008KT 1000 R01L/1400N R19R/1300N R01R/1200D R19L/1400D SN VV007 M01/M01 Q1013 R01L/550157 R08/520155 R01R/12//70 TEMPO 2000 VV010'
'METAR ESSA 181450Z 14008KT 0800 R01L/1200N R19R/1100N R01R/1100N R19L/1400N SN VV006 M01/M01 Q1013 R01L/550147 R08/550255 R01R/12//70 TEMPO 2000 VV010'
'METAR ESSA 181450Z 14008KT 0800 R01L/1200N R19R/1100N R01R/1100N R19L/1400N SN VV006 M01/M01 Q1013 R01L/550147 R08/550256 R01R/12//70 TEMPO VV010'
'METAR ESSA 181450Z 14008KT 0800 R01L/1200N R19R/1100N R01R/1100N R19L/1400N SN VV006 M01/M01 Q1013 R01L/550157 R08/520155 R01R/12//70 TEMPO 2000 VV010'
Thank you for your help!

回答 (1 件)

VBBV
VBBV 2023 年 3 月 3 日
Use match option in place of 'tokens' in the line tokens = regexp(DATALow, '\<(R\d{2}[A-Z]?)/.*?(\d{4})[A-Z]*(?:(?:\d{4})[A-Z])?\>','match') % try using match
DATALow = {'METAR ESOE 180920Z 08006KT 1400 R01/1200VP2000N R19/1700N -SN BKN004 M00/M01 Q1013'
'METAR ESOK 180450Z AUTO 07006KT 1200 R03/P1500N R21/P1500N -SN FEW006/// SCT008/// M02/M03 Q1014'
'METAR ESOK 180520Z AUTO 07006KT 1400 R03/P1500N R21/P1500N -SN FEW006/// M02/M03 Q1014'
'METAR ESOW 180550Z 09005KT 1200 R01/P1500D R19/P1500N SN VV010 M01/M02 Q1016 R01/0///95'
'METAR ESOW 180650Z 09005KT 0800 R01/1300D R19/P1500D SN VV005 M01/M02 Q1016 R01/490232'
'METAR ESOW 180720Z 09005KT 1200 R01/P1500U R19/P1500U SN VV010 M01/M02 Q1016 R01/490232'
'METAR ESOW 181020Z 11006KT 1100 R01/1300N R19/1300N -SN VV010 M01/M01 Q1014 R01/490232'
'METAR ESOW 181050Z 11007KT 1100 R01/1300U R19/1200U -SN VV010 M01/M01 Q1014 R01/490232'
'METAR ESOW 181120Z 11006KT 1100 R01/1200D R19/1200D SN VV010 M01/M01 Q1014 R01/490232'
'METAR ESOW 181150Z 10007KT 1000 R01/1100N R19/1100N SN VV005 M01/M01 Q1013 R01/490232'
'METAR ESOW 181220Z 11007KT 1100 R01/P1500N R19/P1500N -SN VV008 M01/M01 Q1013 R19/690138'
'METAR ESOW 181250Z 11009KT 1200 R01/1300U R19/1300N SN VV009 M01/M01 Q1013 R19/690138'
'METAR ESSA 181320Z 15010KT 1500 -SN VV010 M00/M02 Q1013 R01L/12//66 R08/12//74 R01R/12//70 BECMG 2500 SN BKN014'
'METAR ESSA 181350Z 14008KT 1500 R01L/P1500N R19R/P1500N R01R/1500N R19L/P1500N SN VV013 M01/M01 Q1013 R01L/12//66 R08/12//74 R01R/12//70 TEMPO 4000 VV015'
'METAR ESSA 181350Z 14008KT 1500 R01L/P1500N R19R/P1500N R01R/1500N R19L/P1500N -SN VV013 M01/M01 Q1013 R01L/12//66 R08/12//74 R01R/12//70 TEMPO 4000 VV015'
'METAR ESSA 181420Z 14008KT 1000 R01L/1400N R19R/1300N R01R/1200D R19L/1400D SN VV007 M01/M01 Q1013 R01L/550157 R08/520155 R01R/12//70 TEMPO 2000 VV010'
'METAR ESSA 181450Z 14008KT 0800 R01L/1200N R19R/1100N R01R/1100N R19L/1400N SN VV006 M01/M01 Q1013 R01L/550147 R08/550255 R01R/12//70 TEMPO 2000 VV010'
'METAR ESSA 181450Z 14008KT 0800 R01L/1200N R19R/1100N R01R/1100N R19L/1400N SN VV006 M01/M01 Q1013 R01L/550147 R08/550256 R01R/12//70 TEMPO VV010'
'METAR ESSA 181450Z 14008KT 0800 R01L/1200N R19R/1100N R01R/1100N R19L/1400N SN VV006 M01/M01 Q1013 R01L/550157 R08/520155 R01R/12//70 TEMPO 2000 VV010'}
DATALow = 19×1 cell array
{'METAR ESOE 180920Z 08006KT 1400 R01/1200VP2000N R19/1700N -SN BKN004 M00/M01 Q1013' } {'METAR ESOK 180450Z AUTO 07006KT 1200 R03/P1500N R21/P1500N -SN FEW006/// SCT008/// M02/M03 Q1014' } {'METAR ESOK 180520Z AUTO 07006KT 1400 R03/P1500N R21/P1500N -SN FEW006/// M02/M03 Q1014' } {'METAR ESOW 180550Z 09005KT 1200 R01/P1500D R19/P1500N SN VV010 M01/M02 Q1016 R01/0///95' } {'METAR ESOW 180650Z 09005KT 0800 R01/1300D R19/P1500D SN VV005 M01/M02 Q1016 R01/490232' } {'METAR ESOW 180720Z 09005KT 1200 R01/P1500U R19/P1500U SN VV010 M01/M02 Q1016 R01/490232' } {'METAR ESOW 181020Z 11006KT 1100 R01/1300N R19/1300N -SN VV010 M01/M01 Q1014 R01/490232' } {'METAR ESOW 181050Z 11007KT 1100 R01/1300U R19/1200U -SN VV010 M01/M01 Q1014 R01/490232' } {'METAR ESOW 181120Z 11006KT 1100 R01/1200D R19/1200D SN VV010 M01/M01 Q1014 R01/490232' } {'METAR ESOW 181150Z 10007KT 1000 R01/1100N R19/1100N SN VV005 M01/M01 Q1013 R01/490232' } {'METAR ESOW 181220Z 11007KT 1100 R01/P1500N R19/P1500N -SN VV008 M01/M01 Q1013 R19/690138' } {'METAR ESOW 181250Z 11009KT 1200 R01/1300U R19/1300N SN VV009 M01/M01 Q1013 R19/690138' } {'METAR ESSA 181320Z 15010KT 1500 -SN VV010 M00/M02 Q1013 R01L/12//66 R08/12//74 R01R/12//70 BECMG 2500 SN BKN014' } {'METAR ESSA 181350Z 14008KT 1500 R01L/P1500N R19R/P1500N R01R/1500N R19L/P1500N SN VV013 M01/M01 Q1013 R01L/12//66 R08/12//74 R01R/12//70 TEMPO 4000 VV015' } {'METAR ESSA 181350Z 14008KT 1500 R01L/P1500N R19R/P1500N R01R/1500N R19L/P1500N -SN VV013 M01/M01 Q1013 R01L/12//66 R08/12//74 R01R/12//70 TEMPO 4000 VV015'} {'METAR ESSA 181420Z 14008KT 1000 R01L/1400N R19R/1300N R01R/1200D R19L/1400D SN VV007 M01/M01 Q1013 R01L/550157 R08/520155 R01R/12//70 TEMPO 2000 VV010' } {'METAR ESSA 181450Z 14008KT 0800 R01L/1200N R19R/1100N R01R/1100N R19L/1400N SN VV006 M01/M01 Q1013 R01L/550147 R08/550255 R01R/12//70 TEMPO 2000 VV010' } {'METAR ESSA 181450Z 14008KT 0800 R01L/1200N R19R/1100N R01R/1100N R19L/1400N SN VV006 M01/M01 Q1013 R01L/550147 R08/550256 R01R/12//70 TEMPO VV010' } {'METAR ESSA 181450Z 14008KT 0800 R01L/1200N R19R/1100N R01R/1100N R19L/1400N SN VV006 M01/M01 Q1013 R01L/550157 R08/520155 R01R/12//70 TEMPO 2000 VV010' }
tokens = regexp(DATALow, '\<(R\d{2}[A-Z]?)/.*?(\d{4})[A-Z]*(?:(?:\d{4})[A-Z])?\>','match') % try using match
tokens = 19×1 cell array
{1×2 cell} {1×2 cell} {1×2 cell} {1×2 cell} {1×3 cell} {1×3 cell} {1×3 cell} {1×3 cell} {1×3 cell} {1×3 cell} {1×3 cell} {1×3 cell} {1×1 cell} {1×5 cell} {1×5 cell} {1×7 cell}
tokens{:}
ans = 1×2 cell array
{'R01/1200VP2000N'} {'R19/1700N'}
ans = 1×2 cell array
{'R03/P1500N'} {'R21/P1500N'}
ans = 1×2 cell array
{'R03/P1500N'} {'R21/P1500N'}
ans = 1×2 cell array
{'R01/P1500D'} {'R19/P1500N'}
ans = 1×3 cell array
{'R01/1300D'} {'R19/P1500D'} {'R01/490232'}
ans = 1×3 cell array
{'R01/P1500U'} {'R19/P1500U'} {'R01/490232'}
ans = 1×3 cell array
{'R01/1300N'} {'R19/1300N'} {'R01/490232'}
ans = 1×3 cell array
{'R01/1300U'} {'R19/1200U'} {'R01/490232'}
ans = 1×3 cell array
{'R01/1200D'} {'R19/1200D'} {'R01/490232'}
ans = 1×3 cell array
{'R01/1100N'} {'R19/1100N'} {'R01/490232'}
ans = 1×3 cell array
{'R01/P1500N'} {'R19/P1500N'} {'R19/690138'}
ans = 1×3 cell array
{'R01/1300U'} {'R19/1300N'} {'R19/690138'}
ans = 1×1 cell array
{'R01L/12//66 R08/12//74 R01R/12//70 BECMG 2500'}
ans = 1×5 cell array
{'R01L/P1500N'} {'R19R/P1500N'} {'R01R/1500N'} {'R19L/P1500N'} {'R01L/12//66 R08/12//74 R01R/12//70 TEMPO 4000'}
ans = 1×5 cell array
{'R01L/P1500N'} {'R19R/P1500N'} {'R01R/1500N'} {'R19L/P1500N'} {'R01L/12//66 R08/12//74 R01R/12//70 TEMPO 4000'}
ans = 1×7 cell array
{'R01L/1400N'} {'R19R/1300N'} {'R01R/1200D'} {'R19L/1400D'} {'R01L/550157'} {'R08/520155'} {'R01R/12//70 TEMPO 2000'}
ans = 1×7 cell array
{'R01L/1200N'} {'R19R/1100N'} {'R01R/1100N'} {'R19L/1400N'} {'R01L/550147'} {'R08/550255'} {'R01R/12//70 TEMPO 2000'}
ans = 1×6 cell array
{'R01L/1200N'} {'R19R/1100N'} {'R01R/1100N'} {'R19L/1400N'} {'R01L/550147'} {'R08/550256'}
ans = 1×7 cell array
{'R01L/1200N'} {'R19R/1100N'} {'R01R/1100N'} {'R19L/1400N'} {'R01L/550157'} {'R08/520155'} {'R01R/12//70 TEMPO 2000'}

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by