Formating strings using named tokens

36 vues (au cours des 30 derniers jours)
Vasco
Vasco le 22 Jan 2026 à 8:57
Réponse apportée : Stephen23 le 22 Jan 2026 à 13:55
With regexp a struct can be obtained with named tokens where all information about the token names is in the expression (https://nl.mathworks.com/help/matlab/ref/regexp.html#btqhkwj-7)
Example:
str = '01/11/2000 20-02-2020 03/30/2000 16-04-2020';
expression = ['(?<month>\d+)/(?<day>\d+)/(?<year>\d+)|'...
'(?<day>\d+)-(?<month>\d+)-(?<year>\d+)'];
tokenNames = regexp(str,expression,'names');
for k = 1:length(tokenNames)
disp(tokenNames(k))
end
month: '01' day: '11' year: '2000' month: '02' day: '20' year: '2020' month: '03' day: '30' year: '2000' month: '04' day: '16' year: '2020'
I am now looking for the inverse: specify this expression, and fill in the tokens at the correct location.(like formatting in python https://docs.python.org/3/tutorial/inputoutput.html)
I want to do something like:
tokenName.month = '02'
tokenName.day = '20'
tokenName.year = '2020'
sprintf('(?<month>\d+)/(?<day>\d+)/(?<year>\d+)', tokenName)
Results:
2020/20/02
My usecase is that I define pattern in one location in my scripts to parse filenames, and that in a later moment I want to generate comparable filenames. It is very convinient to have to specify that only once, and not twice with slightly different notation.

Réponses (2)

Star Strider
Star Strider le 22 Jan 2026 à 12:24
I am not exactly certain what you want.
Using an anonymous function is one option --
DMY = @(d,m,y) sprintf('%02d-%02d-%4d',d,m,y);
day = 19;
month = 1;
year = 1807;
str = DMY(day,month,year)
str = '19-01-1807'
.
  1 commentaire
Vasco
Vasco le 22 Jan 2026 à 13:26
Modifié(e) : Vasco le 22 Jan 2026 à 13:26
See original question for a clarification

Connectez-vous pour commenter.


Stephen23
Stephen23 le 22 Jan 2026 à 13:55
Given:
tokenName.month = '02';
tokenName.day = '20';
tokenName.year = '2020';
expr = '(?<month>\d+)/(?<day>\d+)/(?<year>\d+)';
You could do:
out = regexprep(expr, '\(\?<(\w+)>[^)]*\)', '${tokenName.($1)}')
out = '02/20/2020'

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by