Regex for numbers in scientific notation?

I am loading a .obj file with lines like

vn 8.67548e-017 1 -1.55211e-016

for vertex normals. How can I detect them and lead to double notation?

+6
c ++ regex
source share
6 answers

The strtod standard library strtod handles the exponential component just fine (for example, atof , but strtod allows you to distinguish between failed parsing and null value analysis).

+2
source share

A regular expression that will work very well will be:

 -?[\d.]+(?:e-?\d+)? 

Converting to number can be done as follows: String in C ++ scientific notation for double conversion , I think.

Regular expression

 -? # an optional - [\d.]+ # a series of digits or dots (see *1) (?: # start non capturing group e # "e" -? # an optional - \d+ # digits )? # end non-capturing group, make optional 

** 1) This is not 100% correct, technically there can only be one dot, and in front of it there is only one (or not) digit. But practically this should not be. Therefore, regular expression is a good approximation, and false positives should be very unlikely. Feel free to make the regex more specific. *

+5
source share

You can define scientific values ​​using: -?\d*\.?\d+e[+-]?\d+ regex.

+2
source share

If you can be sure that the double format is scientific, you can try something like the following:

  string inp("8.67548e-017"); istringstream str(inp); double v; str >> scientific >> v; cout << "v: " << v << endl; 

If you want to determine if there is a floating point number in this format, then the above expressions will do the trick.

EDIT: the scientific manipulator is not really needed when you switch to double, it will automatically execute your processing (regardless of whether it is installed or scientific)

+2
source share

Well, that’s not quite what you asked for, since it’s not Perl (gak), and this regular definition is not a regular expression, but what I use to recognize the C floating-point literal extension (extension allowing "_" in digital lines), I'm sure you can convert it to an unreadable regular expression if you want:

 /* floats: Follows ISO C89, except that we allow underscores */ let decimal_string = digit (underscore? digit) * let hexadecimal_string = hexdigit (underscore? hexdigit) * let decimal_fractional_constant = decimal_string '.' decimal_string? | '.' decimal_string let hexadecimal_fractional_constant = ("0x" |"0X") (hexadecimal_string '.' hexadecimal_string? | '.' hexadecimal_string) let decimal_exponent = ('E'|'e') ('+'|'-')? decimal_string let binary_exponent = ('P'|'p') ('+'|'-')? decimal_string let floating_suffix = 'L' | 'l' | 'F' | 'f' | 'D' | 'd' let floating_literal = ( decimal_fractional_constant decimal_exponent? | hexadecimal_fractional_constant binary_exponent? ) floating_suffix? 

The C format is intended for programming languages, not data, so it can support something that does not require input.

0
source share

I tried a number of other solutions to no avail, so I came up with this.

  ^(-?\d+)\.?\d+(e-|e\+|e|\d+)\d+$ 

Regular expression visualization

Demo version of Debuggex

Anything that matches is considered valid Scientific Notation.

Note: this accepts e+ , e- and e ; if you do not want to accept e , use this: ^(-?\d+)\.?\d+(e-|e\+|\d+)\d+$

I'm not sure if it works for C ++, but in C # you can add (?i) between ^ and (- in the regular expression to switch the built-in case insensitivity. Without it, exponentials declared as 1.05E+10 , will not be recognized.

Edit: My previous regex was a bit of a mistake, so I replaced it with the previous one.

0
source share

All Articles