Stop the regexp searching to first match

2 vues (au cours des 30 derniers jours)
Giuseppe
Giuseppe le 12 Juin 2023
Réponse apportée : Rik le 12 Juin 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?

Réponse acceptée

Rik
Rik le 12 Juin 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'

Plus de réponses (0)

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by