Home All Groups Group Topic Archive Search About

How to use Regex to Parse this String

Author
7 Apr 2005 3:39 PM
Michael D Murphy
Hi,
I would like to know how to use Regular Expressions to iteratively return
and print the items between the colons in the following string to say the
console..
Any help would be appreciated.
strInputString = "2:Blue:Green:1,2"
Regards,
Michael Murphy

Author
7 Apr 2005 3:48 PM
Marina
I would just use the Split method of the string class, and split by the ':'
character. That returns an array of strings.

Show quoteHide quote
"Michael D Murphy" <mdmur***@scs-techresources.com> wrote in message
news:%23Ve8Fg4OFHA.3788@tk2msftngp13.phx.gbl...
> Hi,
> I would like to know how to use Regular Expressions to iteratively return
> and print the items between the colons in the following string to say the
> console..
> Any help would be appreciated.
> strInputString = "2:Blue:Green:1,2"
> Regards,
> Michael Murphy
>
Author
7 Apr 2005 4:43 PM
Michael D Murphy
Hi Marina,
Thanks for the post, but I was trying to get a little introduction to
Regular Expressions and thought this might be a way to start.
Michael

Show quoteHide quote
"Marina" <someone@nospam.com> wrote in message
news:ejpKPl4OFHA.3880@tk2msftngp13.phx.gbl...
>I would just use the Split method of the string class, and split by the ':'
>character. That returns an array of strings.
>
> "Michael D Murphy" <mdmur***@scs-techresources.com> wrote in message
> news:%23Ve8Fg4OFHA.3788@tk2msftngp13.phx.gbl...
>> Hi,
>> I would like to know how to use Regular Expressions to iteratively return
>> and print the items between the colons in the following string to say the
>> console..
>> Any help would be appreciated.
>> strInputString = "2:Blue:Green:1,2"
>> Regards,
>> Michael Murphy
>>
>
>
Author
8 Apr 2005 2:41 AM
Peter Huang" [MSFT]
Hi Michael,

If we wants to split the string into substring. We will use the split
function, which is also a static function of RegEx function.
        Dim strInputString As String = "2:Blue:Green:1,2"
        For Each str As String In Regex.Split(strInputString, ":")
            Debug.WriteLine(str)
        Next

Here the regex pattern is the :, the regex will match the pattern in the
second parameter of the Split method and then split the string from that
position.


Also we can use the regex to match the substring which did not contain :,
which seems to be an indirect thinking.

        Dim strPattern As String = "[^:]+"
        For Each mt As Match In Regex.Matches(strInputString, strPattern)
            Debug.WriteLine(mt.Value)
        Next

Here is a link about the special character in regex for your reference.

Character Classes
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/ht
ml/cpconcharacterclasses.asp


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.