How to use Regex to Parse this String

  • Thread starter Michael D Murphy
  • Start date
M

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
 
M

Marina

I would just use the Split method of the string class, and split by the ':'
character. That returns an array of strings.
 
M

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
 
P

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top