using regexp with negative and positive floating numbers

140 ビュー (過去 30 日間)
sermet 2017 年 5 月 24 日
data =
% 11x1 cell
to remove the non-numeric character;
data_num = regexp(data,'[0-9]+.[0-9]+', 'match');
In this case, negative signs (-) are also removed. How can I modify this code to get - signs?


Stephen23 2017 年 5 月 24 日
編集済み: Stephen23 2017 年 5 月 24 日
regexp(data,'[+-]?\d+\.?\d*', 'match');
  1. important: . does NOT match the period character, but in fact it matches any character. To only match the period, you need to escape it: \.
  2. \d matches any digit, and is simpler than writing [0-9]
  3. by making the decimal fraction optional my answer will also match integer values
Read the MATLAB documentation for more information:
You might also like to try my FEX submission, which lets you experiment with regular expressions and see their output in real time (as you type):
  5 件のコメント
Alberto Fernandez Osorio
Alberto Fernandez Osorio 2022 年 5 月 11 日
Works great to me! Thanks @Stephen23


その他の回答 (0 件)


Find more on Characters and Strings in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by