简体   繁体   中英

regex match but not include in group

I'm doing a little regex to recognize a complex number. I need it in a C# school program. I must recognize complex numbers like: 3+5i 3+5k 4-i5 6+f7

So, the imaginary part can have any char behind or ahead the value. I wrote this Regex:

(?<reale>[+-]?\d+)(?<immaginaria>[+-]\d+[a-zA-Z]|[+-][a-zA-Z]\d+)

The problem is, when I take the group called "immaginaria" I have got it with the imaginary part char (like i or j) and I'd like to get it without.. I found the solution of using Look-ahead and Look-behind but but I have got a problem while trying to implement it in my regex (It's the first regex I write)

(?<reale>[+-]?\d+)(?<immaginaria>[+-]\d+(?=[a-zA-Z])|[+-](?=[a-zA-Z])\d+)

我对您的第一次尝试做了一些修改,以将虚部的符号和值分开,可以在成功匹配之后将它们连接起来:

(?<reale>[+-]?\d+)((?<immaginariasign>[+-])(?<immaginaria>\d+)[a-zA-Z]|(?<immaginariasign>[+-])[a-zA-Z](?<immaginaria>\d+))

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM