Stop the regexp searching to first match

조회 수: 2 (최근 30일)
Giuseppe
Giuseppe 2023년 6월 12일
답변: Rik 2023년 6월 12일
I have a text a char array axs = 'ACCtl_nEpmNEng_AXIS "Group sampling point for curves (engine speed)" 0x806B139C Epm_nEng Axis_Xs16 32767.50 EngN 4 -16384.00 16383.50 FORMAT "%8.2" EXTENDED_LIMITS -16384.00 16383.50 DEPOSIT ABSOLUTE' and I'm tryng to exctract some information using the command regexp(axs,' +(?<name>\w+) +"(?<description>[^"]*)" +\d+x(?<address>\w+) +(?<input>\w+) +(?<formula>\w+) +\d+(\.\d+)* +\w+ +(?<dimension>\d+)',"names"). The problem is it's returning an empty structure but if the input changes removing 'FORMAT "%8.2"' it gives me what I want:
name 'ACCtl_nEpmNEng_AXIS'
description 'Group sampling point for curves (engine speed)'
address '806B139C'
input 'Epm_nEng'
formula 'Axis_Xs16'
dimension '4'
How can I get the same result also with the original text?

채택된 답변

Rik
Rik 2023년 6월 12일
You're requiring 1 or more spaces at the start of your char array. Therefore, no match actually exists. Removing that requirement (or changing '+' to '*') solves the problem:
axs = 'ACCtl_nEpmNEng_AXIS "Group sampling point for curves (engine speed)" 0x806B139C Epm_nEng Axis_Xs16 32767.50 EngN 4 -16384.00 16383.50 FORMAT "%8.2" EXTENDED_LIMITS -16384.00 16383.50 DEPOSIT ABSOLUTE';
regexp(axs,'(?<name>\w+) +"(?<description>[^"]*)" +\d+x(?<address>\w+) +(?<input>\w+) +(?<formula>\w+) +\d+(\.\d+)* +\w+ +(?<dimension>\d+)',"names")
ans = struct with fields:
name: 'ACCtl_nEpmNEng_AXIS' description: 'Group sampling point for curves (engine speed)' address: '806B139C' input: 'Epm_nEng' formula: 'Axis_Xs16' dimension: '4'

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by