Effacer les filtres
Effacer les filtres

How can I improve my regular expressions?

2 vues (au cours des 30 derniers jours)
Munho Noh
Munho Noh le 30 Oct 2023
Commenté : Munho Noh le 30 Oct 2023
Here are two txt files(frt_susp_sub.txt and rr_susp_sub.txt) and I'd like to extract spring and damper name from each file as shown in the picture below.
This is my code
clc
clear all
str1 = fileread('frt_susp_sub.txt');
frt_spr_assy_xpr = '(?<=SPRING_ASSEMBLY.+?springs.tbl/).+?(?=.spr)';
frt_dpr_assy_xpr = '(?<=DAMPER_ASSEMBLY.+?dampers.tbl/).+?(?=.dpr)';
frt_spr_assy = regexp(str1, frt_spr_assy_xpr, 'match', 'once');
frt_dpr_assy = regexp(str1, frt_dpr_assy_xpr, 'match', 'once');
str2 = fileread('rr_susp_sub.txt');
rr_spr_assy_xpr = '(?<=SPRING_ASSEMBLY.+?springs.tbl/).+?(?=.spr)';
rr_dpr_assy_xpr = '(?<=DAMPER_ASSEMBLY.+?dampers.tbl/).+?(?=.dpr)';
rr_spr_assy = regexp(str2, rr_spr_assy_xpr, 'match', 'once');
rr_dpr_assy = regexp(str2, rr_dpr_assy_xpr, 'match', 'once');
It works well as I expected but the performance is pretty bad.
How can I improve my code?

Réponse acceptée

Stephen23
Stephen23 le 30 Oct 2023
Modifié(e) : Stephen23 le 30 Oct 2023
Get rid of the look-behind expressions. They don't restrict which block of data is being read anyway.
Also note that '.' matches any character, whereas '\.' matches a period character only.
Also get rid of cargo-cult CLC and CLEAR ALL.
str1 = fileread('frt_susp_sub.txt');
frt_spr_assy = regexp(str1, '\w+(?=\.spr)', 'match', 'once')
frt_spr_assy = 'name13'
frt_dpr_assy = regexp(str1, '\w+(?=\.dpr)', 'match', 'once')
frt_dpr_assy = 'name14'
It may be faster to avoid the look-ahead:
tmp = regexp(str1, '(\w+)\.spr', 'tokens', 'once');
frt_spr_assy = tmp{1}
frt_spr_assy = 'name13'
  1 commentaire
Munho Noh
Munho Noh le 30 Oct 2023
Thank you Stephen. It works much faster.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Variables dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by