hamideh
hamideh

Reputation: 225

Regexp equivalent for floating number in matlab

I'd like to know how regexp is used for floating numbers or is there any other function to do this.

For example, the following returns {'2', '5'} rather than {'2.5'}.

nums= regexp('2.5','\d+','match')

Upvotes: 3

Views: 1188

Answers (2)

CitizenInsane
CitizenInsane

Reputation: 4855

@Suever answer has already been accepted, anyway here is some more complete one that should accept all sorts of floating points syntaxes (including NaN and +/-Inf by default):

% Regular expression for capturing a double value
function [s] = cdouble(supportPositiveInfinity, supportNegativeInfinity, 
supportNotANumber)
%[
    if (nargin < 3), supportNotANumber = true; end
    if (nargin < 2), supportNegativeInfinity = true; end
    if (nargin < 1), supportPositiveInfinity = true; end

    % A double
    s = '[+\-]?(?:(?:\d+\.\d*)|(?:\.\d+)|(?:\d+))(?:[eE][+\-]?\d+)?'; %% This means a numeric double

    % Extra for nan or [+/-]inf
    extra = '';
    if (supportNotANumber), extra = ['nan|' extra]; end
    if (supportNegativeInfinity), extra = ['-inf|' extra]; end
    if (supportPositiveInfinity), extra = ['inf|\+inf|' extra]; end

    % Adding capture
    if (~isempty(extra))
        s = ['((?i)(?:', extra, s, '))']; % (?i) => Locally case-insensitive for captured group
    else
        s = ['(', s, ')'];
    end    
%]
end

Basically above pattern says:

  1. Eventually start with '+' or '-' sign
  2. Then followed by either
    • One or more digits followed by a dot and eventually zero to many digits
    • A dot followed by one or more digits
    • One or more digits only
  3. Then followed by exponant pattern, that is:
    • 'e' or 'E' (eventually followed with '+' or '-') and one or more digits

Pattern is later completed with support of Inf, +Inf, -Inf and NaN in case insensitive way. Finally everthing is enclosed between ( and ) for capturing purpose.

Here is some online test example: https://regex101.com/r/K87z6e/1

Or you can test in matlab:

>> regexp('Hello 1.235 how .2e-7 are INF you +.7 doing ?', cdouble(), 'match')

ans = 

    '1.235'    '.2e-7'    'INF'    '+.7'

Upvotes: 1

Suever
Suever

Reputation: 65430

Regular expressions are a tool for low-level text parsing and they have no concept of numeric datatypes. If you will want to parse decimals, you need to consider what characters compose a decimal number and design a regexp to explicitly match all of those characters.

Your example only returns the '2' and '5' because your pattern only matches characters that are digits (\d). To handle the decimal numbers, you need to explicitly include the . in your pattern. The following will match any number of digits followed by 0 or 1 radix points and 0 or more numbers after the radix point.

regexp('2.5', '\d+\.?\d*', 'match')

This assumes that you'll always have a leading digit (i.e. not '.5')

Alternately, you may consider using something like textscan or sscanf instead to parse your string which is going to be more robust than a custom regex since they are aware of different numeric datatypes.

C = textscan('2.5', '%f');
C = sscanf('2.5', '%f');

If your string only contains this floating point number, you can just use str2double

val = str2double('2.5');

Upvotes: 5

Related Questions