Please can you give me some regular expression help

4 Ansichten (letzte 30 Tage)
jlt199
jlt199 am 24 Aug. 2016
Bearbeitet: Thorsten am 24 Aug. 2016
Hi,
I'm trying to extract a number from a string. The string is of the form H970D80Dia4W19L19WT856 and I want to extract the number after the 'D', in this case 80. But regular expressions are confusing me, I can get this far
regexp(file.name,'[D]\d+','match')
Which gives 'D80', but I can't get rid of the 'D'. Can you help me with this? The strings may be different lengths and can have a different number of characters to the left and right of what I want to extract.
Also is there a good cheat sheet I can print for future reference?

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 24 Aug. 2016
regexp('H970D80Dia4W19L19WT856','(?<=D)\d+','match')
  2 Kommentare
jlt199
jlt199 am 24 Aug. 2016
Thank you very much for your quick response
Thorsten
Thorsten am 24 Aug. 2016
Bearbeitet: Thorsten am 24 Aug. 2016
(?<=D) is a "positive look behind assertion" which is itself not part of the match: The match is only valid if there is a "D" in front of it. Nice solution.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Thorsten
Thorsten am 24 Aug. 2016
c = regexp(file.name, 'D(\d+)', 'tokens')
  1 Kommentar
jlt199
jlt199 am 24 Aug. 2016
Thanks for your response too, both answers are very helpful

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by