user525717
user525717

Reputation: 1652

Get values from string .net c# with regex

I want to parse string:

????log L 07/31/2012 - 14:32:50: "Username1<4><STEAM_ID_PENDING><CT>" killed "Username2<2><STEAM_ID_PENDING><TERRORIST>" with "m4a1"

I must get values:

  1. Username1
  2. Username2
  3. m4a1

I have regex pattern

Regex reg = new Regex("[^\"]+\"([^<]+)<[^\"]+\" killed \"([A-Za-z0-9]+)[^\"]+\" with \"([A-Za-z0-9]+)\"");

This works perfectly if second username does not contains _ this or this - If first contains regex getting value.

Please help me to modify my pattern

Thanks

Upvotes: 1

Views: 125

Answers (2)

Ivan Golović
Ivan Golović

Reputation: 8832

Replace it with this:

Regex reg = new Regex("[^\"]+\"([^<]+)<[^\"]+\" killed \"([^<]+)[^\"]+\" with \"([A-Za-z0-9]+)\"")

Group for finding name was:

([A-Za-z0-9]+)

I replaced it with:

([^<]+)

which is a pattern used to find first username.

Upvotes: 1

Cylian
Cylian

Reputation: 11181

Try this

(?<Username1>"Username1(?<data>[^"]*)")\s+killed\s+(?<Username1>"Username2(?<data>[^"]*)")\s+with\s+(?<m4a1>"m4a1(?<data>[^"]*)")

Named groups will return their respective values.

Upvotes: 0

Related Questions