Regex probs...

Reapz

Regular
Joined
Dec 15, 2002
Messages
74
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...

Visual Basic:
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!
 
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
 
Back
Top