TTT
TTT

Reputation: 2375

Regular expression with an optional group

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:

  1. value 'Eline[[email protected]]' normalized 'Eline'
  2. value 'raymond[[email protected]]' normalized 'raymond'

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

Answers (2)

Sina Iravanian
Sina Iravanian

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

stema
stema

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

Related Questions