Stop the regexp searching to first match

4 visualizzazioni (ultimi 30 giorni)
Giuseppe
Giuseppe il 12 Giu 2023
Risposto: Rik il 12 Giu 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?

Risposta accettata

Rik
Rik il 12 Giu 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'

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by