Stop the regexp searching to first match

2 views (last 30 days)
Giuseppe
Giuseppe on 12 Jun 2023
Answered: Rik on 12 Jun 2023
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?

Accepted Answer

Rik
Rik on 12 Jun 2023
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'

More Answers (0)

Categories

Find more on Text Data Preparation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!