dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: VB7
Expertise: Intermediate
Sep 21, 2002

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


SplitQuoted - A split variant that deals correctly with quoted elements

' split a string, dealing correctly with quoted items
'
' TEXT is the string to be split
' SEPARATOR is the separator char (default is comma)
' QUOTES is the character used to quote strings (default is """",
'  the double quote)
'    you can also use a character pair (eg "{}") if the opening
'    and closing quotes are different
'
' for example you can split the following string
'     arr() = SplitQuoted("[one,two],three,[four,five]", , "[]")
' into 3 items, because commas inside []
' are not taken into account

Function SplitQuoted(ByVal Text As String, Optional ByVal Separator As String = _
    ",", Optional ByVal Quotes As String = """") As ArrayList
    ' this is the result 
    Dim res As New ArrayList()
    ' get the open and close chars, escape them for using in regular expressions
    Dim openChar As String = System.Text.RegularExpressions.Regex.Escape _
        (Quotes.Chars(0))
    Dim closeChar As String = System.Text.RegularExpressions.Regex.Escape _
        (Quotes.Chars(Quotes.Length - 1))
    ' build the patter that searches for both quoted and unquoted elements
    ' notice that the quoted element is defined by group #2 
    ' and the unquoted element is defined by group #3
    Dim pattern As String = "\s*(" & openChar & "([^" & closeChar & "]*)" & _
        closeChar & "|([^" & Separator & "]+))\s*"

    ' search all the elements
    Dim m As System.Text.RegularExpressions.Match
    For Each m In System.Text.RegularExpressions.Regex.Matches(Text, pattern)
        ' get a reference to the unquoted element, if it's there
        Dim g3 As String = m.Groups(3).Value
        If Not (g3 Is Nothing) AndAlso g3.Length > 0 Then
            ' if the 3rd group is not null, then the element wasn't quoted
            res.Add(g3)
        Else
            ' get the quoted string, but without the quotes
            res.Add(m.Groups(2).Value)
        End If
    Next
    Return res
End Function
Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date