Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Ok, this is the first time I've used Split with a Regex and I'm having trouble.

 

Below is an example of a string from a CS server log...

 

L 11/01/2003 - 21:25:07: "[sYn]Lastpak<183><STEAM_0:0:976431><TERRORIST>" killed "[sYn]Speedball O.D.<172><STEAM_0:0:316497><CT>" with "glock18"

 

I want to split that string by <STEAM_x:x:xxxxxx> at the same time keeping <STEAM_x:x:xxxxxx> as part of the resulting array.

 

Atm I have the following code...

 

Dim reUID As New System.Text.RegularExpressions.Regex("(<STEAM_.+:.+:.+>)")
Dim Parts = reUID.Split(CurrentLine)

 

But for some reason I'm getting this...

 

Parts(0) = L 11/01/2003 - 21:25:07: "[sYn]Lastpak<183>

Parts(1) = <STEAM_0:0:976431><TERRORIST>" killed "[sYn]Speedball O.D.<172><STEAM_0:0:316497><CT>

Parts(2) = " with "glock18"

 

When what I want to get is this...

 

Parts(0) = L 11/01/2003 - 21:25:07: "[sYn]Lastpak<183>

Parts(1) = <STEAM_0:0:976431>

Parts(2) = <TERRORIST>" killed "[sYn]Speedball O.D.<172>

Parts(3) = <STEAM_0:0:316497>

Parts(4) = <CT>" with "glock18"

 

Cheers,

Andy!

I'm getting the hang of this now... No really I am!
  • *Experts*
Posted

You might try using Groups. You define a group (or multiple groups) in your regular expression string. After you parse you string, it returns multiple Group objects - you can get each string by name that way (much more readable). You'd want 5 groups for your example, maybe more once you learn their power.

 

Now, the niceness of groups comes at the expense of speed. If you were to do the same logic in code using IndexOf and Substring, it would be MUCH faster, but much less readable. Having parsed some large log files before, I know that sometimes speed is more important. At least you have options :)

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...