Marvin Dickhaus
Marvin Dickhaus

Reputation: 805

vb.net Regexp Split comma separated String

I have a String, which contains a list of mail addresses like so:

Dim address1 As String = """Merkel, Angela"" <[email protected]>, ""Peter Altmeyer"" <[email protected]>"

what I'm trying to archieve is to separate the String at the comma. I figure I need Regexp.Split therefore, but I don't have a clue, what exactly I have to do to get the output array of

"Merkel, Angela" <[email protected]>
"Peter Altmeyer" <[email protected]>

I'm especially confused by the double quotation mark "" to escape the quotation mark. Is this also escaped like so in the regular expression?

Upvotes: 0

Views: 729

Answers (3)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112299

You can simply do it with the String.Split method, by including the ">" in the separator (">, "); however the ">" will be missing from the result and will have to re-add it.

With Regex you can do it as follows:

Dim parts() As String = Regex.Split(address1, "(?<=>),\s")

Here I am using the Regex pattern

(?<=prefix)find

which finds a position following a prefix. The result does not include the prefix. Therefore only ", " is removed from the output and the ">" remains.

Upvotes: 1

Badri
Badri

Reputation: 55

Dim address1 As String = """Merkel, Angela"" <[email protected]>, ""Peter Altmeyer""     <[email protected]>"
Dim parts() As String = Regex.Split(address1, "(?<=>),\s*?(?="")")

Upvotes: 0

tenub
tenub

Reputation: 3446

You can split on this RegEx: (?<=>),\s*?(?=""). It finds commas (with zero or more whitespaces after) preceded by a < and proceeded by a "".

Upvotes: 1

Related Questions