필터 지우기
필터 지우기

Edit user input

조회 수: 1 (최근 30일)
Raldi
Raldi 2011년 12월 1일
Lets say i have something like s='12s+34a+45'; Is there a way so i can only store the numbers that are in front of the letter s and store it to an array?

채택된 답변

Walter Roberson
Walter Roberson 2011년 12월 1일
numstrs = regexp(TheString, '-?\d+(?=\s*s)', 'match');
and convert the resulting cells strings in to numeric forms.
The (?=\s*s) part is regexp-ese for "the expression after the '=' until the ')' must be matched in the input right after whatever was matched before this, but do not return this part as part of the match". Another way of phrasing this is that it "looks ahead" for the pattern but does not include it as part of what is matched.
\s*s stands for "any number of whitespace characters (including none), followed by the letter 's')
  댓글 수: 1
Raldi
Raldi 2011년 12월 1일
Thanks Walter. This is way much better than what i came up with

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

추가 답변 (1개)

Raldi
Raldi 2011년 12월 2일
There is a problem though with this code. It seems it wont recognise lets say 0.5*s but it will only take number 5, so i need a way to exclude '.' from regexp.
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 12월 2일
If you define exactly which patterns of characters are to be accepted, then a regexp() expression can be constructed. The pattern to match a general floating point number that might be in exponential form gets quite complicated though.
numstrs = regexp(TheString, '-?(?:(?:\d*\.)?\d+|\d+\.)(?=\s*s)', 'match')
The above does not recognize exponentials, but it does recognize
optional leading minus sign, recognizes digits followed by 's', recognizes digits followed by '.s', recognizes digits followed by '.' followed by digits followed by 's', recognizes '.' followed by digits followed by 's', while still refusing '.s' alone with no digits. Oh yes, optional spaces are permitted before the 's' itself.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by