简体   繁体   中英

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"" <angela@example.com>, ""Peter Altmeyer"" <peter@example.com>"

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" <angela@example.com>
"Peter Altmeyer" <peter@example.com>

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

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.

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

Dim address1 As String = """Merkel, Angela"" <angela@example.com>, ""Peter Altmeyer""     <peter@example.com>"
Dim parts() As String = Regex.Split(address1, "(?<=>),\s*?(?="")")

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