Regular expressions: number that does not begin or end with a letter

조회 수: 10(최근 30일)
L_Del
L_Del 2019년 6월 13일
답변: L_Del 2019년 6월 13일
Hi
This is my first post here.
I'm trying to read a file that has strings like this one c0, ("cee zero comma") but it has other strings like 117. So I need to create a regular expression that only selects numbers that don't have any non numbers in front of them. Makes sense?
How can I select only the 0?
I have tried ^(?![\D][0-9]+ without success. I know this doesn't do anything at the end.
In simpler terms, what I'm trying to achieve is this: use regexp to only select decimal numbers and integers (but the interger part should not select integers that are in the decimal number...) .
Here is one of the lines I'm working on: d="M117.125,310.375c0,-77.729,80.738,-140.625,180.515,-140.625" />

채택된 답변

Debasish Samal
Debasish Samal 2019년 6월 13일
편집: Debasish Samal 2019년 6월 13일
Here is a solution to your answer. This solution separates all the decimal numbers/integers from the non integers/decimals.
str = "M117.125,310.375c0,-77.729,80.738,-140.625,180.515,-140.625" ;
exp1 = ',';
splitStr = regexp(str,exp1,"split");
exp3 = '[^0-9.,-]'
splitStr = regexprep(splitStr,exp3,'')
Output:
"117.125" "310.3750" "-77.729" "80.738" "-140.625" "180.515" "-140.625"
If this is what you needed, try the above regexp.
If you dont split the string at the commas you get this output.
splitStr = "117.125,310.3750,-77.729,80.738,-140.625,180.515,-140.625"

추가 답변(1개)

L_Del
L_Del 2019년 6월 13일
Hi
Thank you for your prompt answer. This is almost perfect. I just changed the exp1 line into this:
exp1 = '(,|c)';
and now it takes the c character into account!
Again, many thanks for getting me unstuck!

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by