2016-11-05 8 views
1

Matlab regexp를 사용하여 xml 파일을 구문 분석하려고합니다. 특히 저는 "deposits"와 "/ deposits"사이에서 발생하는 "curvepoint"라는 단어의 모든 배열을 검색하고 싶습니다. 는 XML에 대한 그래서 것은이다른 두 단어 사이의 정규식 일치하는 단어 (Matlab regexp)

같은 [6x1] 배열이 아래에 "

<curvepoint> 
<curvepoint> 
<curvepoint> 
<curvepoint> 
<curvepoint> 
<curvepoint> 

"

각 "curvepoint 사이에 산재 다른 텍스트의 많은이 작동하지 않습니다 아래 내 시도 "단어 부각 및보기 전방/후퇴, 그러나 나는 이것을 취급하는 방법을 모른다.

regexp(XMLText,'(?<=<deposits>)(<curvepoint>)(?=</deposits>)','match')' 

XMLTEXT는

<?xml version="1.0" encoding="utf-8"?> 
<interestRateCurve> 
    <effectiveasof>2016-11-07</effectiveasof> 
    <currency>EUR</currency> 
    <baddayconvention>M</baddayconvention> 
    <deposits> 
     <daycountconvention>ACT/360</daycountconvention> 
     <snaptime>2016-11-04T15:00:00.000Z</snaptime> 
     <spotdate>2016-11-09</spotdate> 
     <calendars> 
     <calendar>none</calendar> 
     </calendars> 
     <curvepoint> 
     <tenor>1M</tenor> 
     <maturitydate>2016-12-09</maturitydate> 
     <parrate>-0.00373</parrate> 
     </curvepoint> 
     <curvepoint> 
     <tenor>2M</tenor> 
     <maturitydate>2017-01-09</maturitydate> 
     <parrate>-0.00339</parrate> 
     </curvepoint> 
     <curvepoint> 
     <tenor>3M</tenor> 
     <maturitydate>2017-02-09</maturitydate> 
     <parrate>-0.00312</parrate> 
     </curvepoint> 
     <curvepoint> 
     <tenor>6M</tenor> 
     <maturitydate>2017-05-09</maturitydate> 
     <parrate>-0.00213</parrate> 
     </curvepoint> 
     <curvepoint> 
     <tenor>9M</tenor> 
     <maturitydate>2017-08-09</maturitydate> 
     <parrate>-0.0013</parrate> 
     </curvepoint> 
     <curvepoint> 
     <tenor>1Y</tenor> 
     <maturitydate>2017-11-09</maturitydate> 
     <parrate>-0.00071</parrate> 
     </curvepoint> 
    </deposits> 
    <swaps> 
     <fixeddaycountconvention>30/360</fixeddaycountconvention> 
     <floatingdaycountconvention>ACT/360</floatingdaycountconvention> 
     <fixedpaymentfrequency>1Y</fixedpaymentfrequency> 
     <floatingpaymentfrequency>6M</floatingpaymentfrequency> 
     <snaptime>2016-11-04T15:00:00.000Z</snaptime> 
     <spotdate>2016-11-09</spotdate> 
     <calendars> 
     <calendar>none</calendar> 
     </calendars> 
     <curvepoint> 
     <tenor>2Y</tenor> 
     <maturitydate>2018-11-09</maturitydate> 
     <parrate>-0.00157</parrate> 
     </curvepoint> 
     <curvepoint> 
     <tenor>3Y</tenor> 
     <maturitydate>2019-11-09</maturitydate> 
     <parrate>-0.00115</parrate> 
     </curvepoint> 
     <curvepoint> 
     <tenor>4Y</tenor> 
     <maturitydate>2020-11-09</maturitydate> 
     <parrate>-0.00059</parrate> 
     </curvepoint> 
     <curvepoint> 
     <tenor>5Y</tenor> 
     <maturitydate>2021-11-09</maturitydate> 
     <parrate>0.00017</parrate> 
     </curvepoint> 
     <curvepoint> 
     <tenor>6Y</tenor> 
     <maturitydate>2022-11-09</maturitydate> 
     <parrate>0.00108</parrate> 
     </curvepoint> 
     <curvepoint> 
     <tenor>7Y</tenor> 
     <maturitydate>2023-11-09</maturitydate> 
     <parrate>0.0021</parrate> 
     </curvepoint> 
     <curvepoint> 
     <tenor>8Y</tenor> 
     <maturitydate>2024-11-09</maturitydate> 
     <parrate>0.00316</parrate> 
     </curvepoint> 
     <curvepoint> 
     <tenor>9Y</tenor> 
     <maturitydate>2025-11-09</maturitydate> 
     <parrate>0.00419</parrate> 
     </curvepoint> 
     <curvepoint> 
     <tenor>10Y</tenor> 
     <maturitydate>2026-11-09</maturitydate> 
     <parrate>0.00513</parrate> 
     </curvepoint> 
     <curvepoint> 
     <tenor>12Y</tenor> 
     <maturitydate>2028-11-09</maturitydate> 
     <parrate>0.00673</parrate> 
     </curvepoint> 
     <curvepoint> 
     <tenor>15Y</tenor> 
     <maturitydate>2031-11-09</maturitydate> 
     <parrate>0.00838</parrate> 
     </curvepoint> 
     <curvepoint> 
     <tenor>20Y</tenor> 
     <maturitydate>2036-11-09</maturitydate> 
     <parrate>0.00966</parrate> 
     </curvepoint> 
     <curvepoint> 
     <tenor>30Y</tenor> 
     <maturitydate>2046-11-09</maturitydate> 
     <parrate>0.01006</parrate> 
     </curvepoint> 
    </swaps> 
</interestRateCurve> 

답변

0

가 XML을 구문 분석하는 정규식을 사용하지 않습니다. 기껏해야 솔루션은 부서지기 쉽습니다. 대신 실제 XML 파서를 사용하십시오.

MATLAB에서 XML을 읽고, 쓰고, 변환하는 데는 xmlread, xmlwritexslt 함수를 사용하십시오.

MathWorks blog has XML posts은 MATLAB에서 이러한 함수를 사용합니다.

+0

감사합니다 kj, 나는 이것을 달성하기 위해 matlab의 xml 기능에 대해 작업 할 시간이 없다 (이것은 나의 "프로젝트"의 아주 작은 부분 임). 나는 또한 정규 표현식의 논리가 막연한 것처럼 정규 표현식이 깨지기 쉽다고 생각한다. 정규 표현식 경험이 풍부한 코더는 엄격한 표현을 찾을 수 있어야한다. 나는이 질문을 특정한 해결책을 얻으려는 노력에서 아주 구체적으로 만드는 데 시간을 보냈다. – user152112

+0

원하는대로 해보십시오. 근본적으로나 실제적으로 XML을 파싱하기위한 불일치 정규 표현식의 정확성을 정확히 알고 있다면이 질문을하지 않아도됩니다. 이러한 위험은 다른 곳에서도 잘 설명되어 있습니다. 나는 당신이 옳은 방식으로 그렇게하는 법을 배워야하는 것보다 위험을 재촉 할 시간이 적다. – kjhughes

관련 문제