Regexp to pull numerics only

조회 수: 8 (최근 30일)
Stephen
Stephen 2019년 7월 3일
댓글: Stephen 2019년 7월 4일
I'm using regexp to pull a version number from a text file:
returned = string(regexp (filetext, '(?<=Version_String )[A-Z]*\d*.\d*.\d*.\d*', 'match'))
This returns a string
"YZ1.1.2.3"
which is good.
When I try and extract just the numbers and decimal point from this string with
version = regexp (returned, '\d*.\d*.\d*.\d*', 'match')
I get:
version = 1x2 string array "YZ1.1" ".2.3"
which is not what I was expecting. I was hoping for 1.1.2.3
Can someone explain why this is returned and possibly how to fix it please?
edit: Apologies to all, I missed the decimal points after the \d, I have corrected. I wrote this on my phone in my lunch break!
  댓글 수: 1
Stephen23
Stephen23 2019년 7월 3일
편집: Stephen23 2019년 7월 3일
I don't see anything in any of your regular expressions that would match the points.
When I test your first example, I do not get the result that you have shown us:
>> S = 'A B C Version_String YZ1.1.2.3 X Y Z';
>> regexp(S, '(?<=Version_String )[A-Z]*\d*\d*\d*\d*', 'match')
ans =
'YZ1'
which is exactly what I would expect (as the matching fails at the first dot character).

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

채택된 답변

Guillaume
Guillaume 2019년 7월 3일
  • \d*\d*\d*\d* is exactly the same as \d*, match 0 or more digits. It probably would be better as \d+, match 1 or more digits
  • That regular expression cannot return anything with letters or dots in it, so it's clearly not what you are using.
  댓글 수: 1
Stephen
Stephen 2019년 7월 4일
Yes, thank you, disregarding the errors I made when writing up the question, the problem was using * instead of +

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by