Regular Expressions 101

Community Patterns

Complex numbers supporting scientific notation and extensive error-checking

0

Regular Expression
PCRE (PHP <7.3)

/
^(?<real>(?<decimal>[+-]?(?:\d*(?:\.(?=\d))?\d+))(?:(?:(?:(?<=\d)[Ee])?((?&decimal)))(?<=\d))?)(?:(?<=\d)[, ](?=\d|[+-]|\.)((?&real))[iIjJ])?$
/
g

Description

The regex verifies complex numbers in quite a few mixtures of different floating point representations, with extensive error checking - however, there may be edge cases which i haven't addressed, so feel free to put in more unit tests!

Submitted by anonymous - 7 years ago