Please can you give me some regular expression help

조회 수: 4 (최근 30일)
jlt199
jlt199 2016년 8월 24일
편집: Thorsten 2016년 8월 24일
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?

채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 8월 24일
regexp('H970D80Dia4W19L19WT856','(?<=D)\d+','match')
  댓글 수: 2
jlt199
jlt199 2016년 8월 24일
Thank you very much for your quick response
Thorsten
Thorsten 2016년 8월 24일
편집: Thorsten 2016년 8월 24일
(?<=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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Thorsten
Thorsten 2016년 8월 24일
c = regexp(file.name, 'D(\d+)', 'tokens')
  댓글 수: 1
jlt199
jlt199 2016년 8월 24일
Thanks for your response too, both answers are very helpful

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by