I'm trying to write a regular expression to match the input of complex number in the following forms: (With a,b real number, and i/I imaginay unit)
a
a+bi
a-bi
+bi
-bi
a+i
ai
i
-i
Of course in all numbers I want to be able to also read exponential form (eg: 1.23e+45-67.89e-1256i). I've come up with this:
regex aplusbi ("(([\\+-]?[\\d]+([\\.][\\d]+)?)?([eE][\\+-]?[\\d]+)?)?(([\\+-])?(([\\d]+?([\\.][\\d]+)?)?([eE]?[\\+-]?[\\d]+)?)?[iI])?")
It gets most of them correct, however when I input +bi or -bi or simply bi the b part goes into the real one, and it also recognises this number as correct:
12.418.546i
getting 12.41 into the real part and 8.546 into the imaginary one. How could I correct it? I'm kind of new to C++ and regexes so any kind of help would be appreciated, thanks!
Since you have like real/imaginary clusters, you'd have to introduce 2 assertions
to control it.
Raw: ^(?=[iI.\\d+-])([+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE][+-]?\\d+)?(?![iI.\\d]))?([+-]?(?:(?:\\d+(?:\\.\\d*)?|\\.\\d+)(?:[eE][+-]?\\d+)?)?[iI])?$
Stringed: "^(?=[iI.\\\\d+-])([+-]?(?:\\\\d+(?:\\\\.\\\\d*)?|\\\\.\\\\d+)(?:[eE][+-]?\\\\d+)?(?![iI.\\\\d]))?([+-]?(?:(?:\\\\d+(?:\\\\.\\\\d*)?|\\\\.\\\\d+)(?:[eE][+-]?\\\\d+)?)?[iI])?$"
https://regex101.com/r/0JMEZ8/1
Readable version
^
(?= [iI.\d+-] ) # Assertion that keeps it from matching empty string
( # (1 start), Real
[+-]?
(?:
\d+
(?: \. \d* )?
| \. \d+
)
(?: [eE] [+-]? \d+ )?
(?! [iI.\d] ) # Assertion that separates real/imaginary
)? # (1 end)
( # (2 start), Imaginary
[+-]?
(?:
(?:
\d+
(?: \. \d* )?
| \. \d+
)
(?: [eE] [+-]? \d+ )?
)?
[iI]
)? # (2 end)
$
Regex: ^(?:(?<real>\\d+(?:(?:\\.\\d+)?(?:e[+\\-]\\d+)?)?)?(?:[+\\-]))?(?<imaginary>\\d+(?:(?:\\.\\d+)?(?:e[+\\-]\\d+)?)?)?[iI]$
So the above was a good start, but I needed some improvements. For instance, Matlab writes out complex numbers in .csv files in the form -0.0232877540359511+-0.00509035792974122i, which could not read because of the leading sign. I also wanted to handle the case where the exponent character is E instead of e, and the exponent has no sign (default positive) so here is a small adjustment to properly handle these additional cases:
^(?:(?<real>[+\-]?\d+(?:(?:\.\d+)?(?:[eE][+\-]?\d+)?)?)?(?:[+\-]))?(?<imaginary>[+\-]?\d+(?:(?:\.\d+)?(?:[eE]?[+\-]\d+)?)?)?[iI]$
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.