MATLAB Answers


using regexp with negative and positive floating numbers

Asked by sermet on 24 May 2017
Latest activity Edited by Stephen Cobeldick on 24 May 2017
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?


Sign in to comment.

1 Answer

Answer by Stephen Cobeldick on 24 May 2017
Edited by Stephen Cobeldick on 24 May 2017
 Accepted Answer

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):


Show 1 older comment
Why would \d+\d* be a problem? My tests showed that it correctly matches any integer, exactly as I intended, and I don't see how it would cause any problems for this use case. Can you please explain.
It is redundant -- you already matched all the possible digits in a row with the \d+ and then you spin looking for optional digits.
@Walter Roberson: the tests I just ran showed that your code is actually slightly slower (1e5 iterations):
Elapsed time is 3.67537 seconds. % \.?\d*
Elapsed time is 3.91039 seconds. % (\.\d*)?
I have no idea how TMW implements this, or whether it would be stable between versions...

Sign in to comment.