Reputation: 2375
I want to split a comma seperated list of email addresses AND I want to get the user friendly names within those email addresses if there is one.
Now I use this regular expression:
(?<value>(?<normalized>.*?)\[.*?\])\s*,*\s*
This reg exp works for input string
"Eline[[email protected]],raymond[[email protected]]"
It returns two pairs:
but it doesn't work for input string
"Eline[[email protected]],[email protected],raymond[[email protected]]"
It should return 3 email addresses with normalized empty in the second case.
Upvotes: 0
Views: 130
Reputation: 16296
You can try this pattern:
(?<value>(?<normalized>[^\[,]*?)\[?[^,]*\]?)
It seems that your pattern is not intended to match the whole input string, and you intent to iterate through different matches, therefore there's no need to add the patterns for commas in the end.
The normalized group matches characters while they are not either [
or ,
. The group for value makes [
, and ]
optional, and matches any character in between while they are not a comma.
Upvotes: 0
Reputation: 93086
Why should your second example return 3 matches? The second email has no [...], which you require in your pattern, so this address is additionally matched by (?<normalized>.*?)
of the third email address.
Try this here instead:
(?<value>(?<normalized>[^,]*?)\[.*?\]|[^,\[\]]*)\s*,?\s*
See it here on Regexr
But this is getting unreadable, why not at first split on commas and work then on the resulting array?
Upvotes: 1