How to extract a specific series of strings from a character array
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi guys,
I have a massive character array (1x187253) that I imported in matlab. Here is a small sample:
NEW SCOMPONENT /JBRC200XT
DESC 'CARBON STEEL ORDINARY REDUCER JIS3452 BWD CON. 350Ax200A'
GTYP REDU
PARA 350 200 355.6 216.3 BWD $
330.2 0
END
NEW SCOMPONENT /JBRC200XV
DESC 'CARBON STEEL ORDINARY REDUCER JIS3452 BWD CON. 350Ax250A'
GTYP REDU
PARA 350 250 355.6 267.4 BWD $
330.2 0
END
What I want to do with this character array is obtain all the 9 letter codes that are located right next to the NEW SCOMPONENT row (e.g. JBRC200XT and JBRC200XV in this case) as well as the characters between the quotes that are located on the DESC line (e.g. 'CARBON STEEL ORDINARY REDUCER... ') and place those side by side on a table in matlab table to be exported in excel.
I know that this should be possible however I have been trying for the last few days being stuck in the first step of even obtaining the codes JBRC200...
Thanks for your help in advance,
KR,
KMT.
댓글 수: 0
채택된 답변
TADA
2018년 11월 2일
편집: TADA
2018년 11월 2일
Regular expressions are your friends
regexp(text, 'NEW SCOMPONENT\s*\/?(?<component>[\w]{9})\s+DESC\s*''(?<desc>[^'']+)', 'names');
the result is a struct array with two fields - component and desc containing your strings
ans =
struct with fields:
component: 'JBRC200XT'
desc: 'CARBON STEEL ORDINARY REDUCER JIS3452 BWD CON. 350Ax200A'
I tried it on your sample which i duplicated into a giant text file and it works very fast
댓글 수: 6
TADA
2018년 11월 2일
I'll leave the details to you, but you can use the \d to match digits and use the curly bracers with comma separated numbers to specify range in the number of repeats.
You can find some decent regex testers online to help with planning the right regex. Some of them also have good modules for learning regex patterns.
You should know that there are slight differences in the features of regex engines between different environments (Matlab, python, js, etc.), but the patterns are identical to the most part.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!