Sponsors

There are currently no sponsors. Become a sponsor today!

0

ECMAScript (JavaScript)

/

([+-]?)\s*(?:(?:(?:[^.([{]?(\d+))|(\d+\.\d+(?!\w+)))(?!\w+|\.))(?![([{])

/

g

I created this regex to match all integers and floats in polynomials.

This regex MATCHES:

- all integers
- all floats

All of the above + " not followed by "

- open parenthesis
- letters

GROUPS returned:

- SIGN: the sign of the integer or float matched, and empty string is returned if the sign is not provided
- INT: matches this group if the number is an integer
- FLOAT: matches this group if the number is a float

Note:

- the name of the groups are up to you, however "sign, int and float" are the most appropriate
- if the number is an integer, the float group (3) will NOT be matched
- if the number is a float, the integer group (2) will NOT be matched

so be careful when dealing with the order of the groups returned. In many languages it returns undefined/null, anyway just keep in mind that the order of the groups is SIGN, INT, FLOAT.

Probably I'll create another version where it doesn't matter if the number returned is int or float, but I think I'll leave it as it is because it's more precise.

Submitted by Giuseppe Tavella - 7 years ago