tags:

views:

52

answers:

3

I want to have a string in the following format

"FAG001 FAG002 FAG003"

and want to split it into

"FAG001" "FAG002" "FAG003"

using a regular expression. Unfortunately my knowledge of regular expression synatax is limited to say teh least. I have tried things like

Dim result = Regex.Split(npcCodes, "([A-Z]3[0-9]3)").ToList

without luck

+4  A: 

No need of regex here, you could use String.Split

Dim result As String() = npcCodes.Split(new Char[]{" "})

But if you really want to use regex :

Dim result = Regex.Split(npcCodes, " ").ToList()
madgnome
Your regular expression split gives the result `"", "FAG001", " ", "FAG002", " ", "FAG003", ""`, i.e. you are really getting what's between the values, and the values are also included as you catch what you split on...
Guffa
Thanks. Corrected ^^
madgnome
Thanks, I'm using a regex because the format I describe above may not be the only format the string is received in
simon_bellis
+2  A: 

As madgnome has pointed out you don't need regular expressions here if the string is always separated with spaces.

However for your information the error you made was that you need curly braces for numeric quantifiers:

[A-Z]{3}

And instead of Regex.Split you can uses Regex.Matches.

Mark Byers
A: 

The regular expression to use in the Split method would be really simple:

Dim result = Regex.Split(npcCodes, " ").ToList

As the expression only matches a single character, you can just as well use the regular Split method in the String class:

Dim result = npcCodes.Split(" "C).ToList
Guffa