フィルターのクリア

Get number from a string

15 ビュー (過去 30 日間)
Lucas
Lucas 2012 年 9 月 12 日
I have a string that looks like this
I_am_a_String.562
I need to be able to pull the number out of that, and I was using the following to do so:
num = regexp(str, '\d+', 'match');
and that worked will I ran into strings like:
When lpdlogic21 is true....
and I started getting numbers that I didn't want, so I switched it to this:
num = regexp(lines{1}{jj}, '(?<words>\w*).(?<ID>\d*)', 'names');
but this didn't work for the off string either. It gave me num with about 20 entries in it. So my question is what can I use so I only get answers for the first string but none of the others. This is a basic question and I know I should know this by now, but I'm drawing a blank right now.
  1 件のコメント
Matt Fig
Matt Fig 2012 年 9 月 12 日
What others? You need to give a rule for deciding when to return a number and when not to return a number.

サインインしてコメントする。

採用された回答

Matt Fig
Matt Fig 2012 年 9 月 12 日
編集済み: Matt Fig 2012 年 9 月 12 日
What do you mean it didn't work. Seems to work here. So what numbers do you want? Only numbers that are after a '.' or what (use: '\.\d+')? Be specific!
str = 'When lpdlogic21 is true....';
num = regexp(str, '\d+', 'match')
num =
'21'
  1 件のコメント
Matt Fig
Matt Fig 2012 年 9 月 12 日
編集済み: Matt Fig 2012 年 9 月 12 日
O.k., so the rule is: if the string has any spaces whatsoever, return nothing. If it has no spaces return all numbers following a decimal. Got it.
num = {};
if ~any(isspace(str))
num = regexp(T, '(?<=\.)\d+', 'match');
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by