I just put here the solution, to a problem I encountered during my development, in order to have a maximum of feedback from your experience on regex. The goal is to learn how to reduce the size of regex and improve their flexibility.
Please in educational purposes, explain your corrections.
The probleme was :
I got a string that can contains some example of value
- test@test.com
- test@test.com;
- test@test.com;test@test.com
- test@test.com;test@test.com;
And this string must be validate with a regex, in order to obtains differents adress mail separate by ";"
I arrive to this result :
^(?:((?:[a-zA-Z0-9_\-\.]+)@(?:[a-zA-Z0-9\-]+\.)(?:[a-zA-Z]{2,3}))(\;)?$|(((?:[a-zA-Z0-9_\-\.]+)@(?:[a-zA-Z0-9\-]+\.)(?:[a-zA-Z]{2,3}))(?:\;))+((?:[a-zA-Z0-9_\-\.]+)@(?:[a-zA-Z0-9\-]+\.)(?:[a-zA-Z]{2,3}))(\;)?$)
If this regex is so long, it's because this example :
- test@test.comtest@test.com;
Pass with my smaller first regex, and he must not pass.
I know that i can reduce [a-zA-Z0-9_-.] with \w but it's not my main problem.
Is there one way to make some "groups" reusable in order to write only one time
((?:[a-zA-Z0-9_\-\.]+)@(?:[a-zA-Z0-9\-]+\.)(?:[a-zA-Z]{2,3}))
and reuse it into the same regex ?
The language is PHP but the question is broader on regular expressions in general.
Thanks a lot