Regular Expressions 101

Community Patterns

Complex numbers supporting scientific notation and extensive error-checking

0

Regular Expression
PCRE (PHP <7.3)

/
^(([+-]?(?:\d*(?:\.(?=\d))?\d+))(?:(?:(?:(?<=\d)[Ee])?((?2)))(?<=\d))?)(?:(?<=\d)[, ](?=\d|[+-]|\.)((?1))[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