0

I've been working on trying to get this string split in a couple different places which I managed to get to work, except if the name had a forward-slash in it, it would throw all of the groups off completely.

The string:

123.45.678.90:00000/98765432109876541/[CLAN]PlayerName joined [windows/12345678901234567]

I essentially need the following:

  • IP group: 123.45.678.90:00000 (without the following /)
  • id group: 98765432109876541
  • name group: [CLAN]PlayerName
  • id1 group: 12345678901234567

The text "joined" also has to be there. However windows does not.

Here is what I have so far:

(?<ip>.*)\/(?<id>.*)\/(.*\/)?(?<name1>.*)( joined.*)\[(.*\/)?(?<id1>.*)\]

This works like a charm unless the player name contains a "/". How would I go about escaping that?

Any help with this would be much appreciated!

5
  • Easiest solution: don't allow a / in player names. Or, translate a / in player name to something like %SLASH% and then display it as a / accordingly. Commented Jun 16, 2016 at 17:08
  • 2
    In the future, please post questions with a helpful title, "More than likely an easier way to do this. Probably a very simple fix" tells us nothing about the question. Commented Jun 16, 2016 at 17:09
  • @dubstylee you're assuming he has control over that. My guess, given the example, he's scraping from somewhere and therefore has no control over whether or not slashes are allowed. Commented Jun 16, 2016 at 17:09
  • 1
    Try @"^(?<ip>[\d.:]*)/(?<id>\d+)/(?<name1>\S+) joined \[\w+/(?<id1>\d+)]$" pattern. See demo. Commented Jun 16, 2016 at 17:12
  • Using '.*' create issues because it goes to end of string. Terminate all groups with a not like following : string pattern = @"(?<ip>[^/]*)/(?<id>[^/]*)/(?<name1>[^\s]*)\s*(?<op>[^\s]*)[^/]*/(?<id1>[^]]*)"; Commented Jun 16, 2016 at 17:19

3 Answers 3

1

Since you tag your question with C# and Regex and not only Regex, I will propose an alternative. I am not sure if it will more efficient or not. I find it easiest to read and to debug if you simply use String.Split():

Demo

public void Main()
{
    string input = "123.45.678.90:00000/98765432109876541/[CLAN]Player/Na/me joined [windows/12345678901234567]";

    // we want "123.45.678.90:00000/98765432109876541/[CLAN]Player/Na/me joined" and "12345678901234567]"
    // Also, you can remove " joined" by adding it before " [windows/"
    var content = input.Split(new string[]{" [windows/"}, StringSplitOptions.None);

    // we want ip + groupId + everything else
    var tab = content[0].Split('/');

    var ip = tab[0];
    var groupId = tab[1];
    var groupName = String.Join("/", tab.Skip(2)); // merge everything else. We use Linq to skip ip and groupId
    var groupId1 = RemoveLast(content[1]); // cut the trailing ']'

    Console.WriteLine(groupName);
}

private static string RemoveLast(string s)
{
    return s.Remove(s.Length - 1);
}

Output:

[CLAN]Player/Na/me joined

If you are using a class for ip, groupId, etc. and I guess you do, just put everything in it with a constructor which accept a string as parameter.

Sign up to request clarification or add additional context in comments.

1 Comment

I always lean towards String.Split over a regex for speed reasons, but I don't think your code solves the "what if a player name contains a slash" problem.
0

You shouldn't be using greedy quanitifiers (*) with an open character such as .. It won't work as intended and will result in a lot of backtracking.

This is slightly more efficient, but not overly strict:

^(?<ip>[^\/\n]+)\/(?<id>[^\/]+)\/(?<name1>\S+)\D+(?<id1>\d+)]$

Regex demo

Comments

0

You basically needs to use non greedy selectors (*?). Try this:

(?<ip>.*?)\/(?<id>.*?)\/(?<name1>.*?)( joined )\[(.*?\/)?(?<id1>.*?)\]

6 Comments

This worked like a charm. Always something so simple. I will accept once timer is up.
In C# regex patterns, you do not need to escape the / symbol. Do not use regex101 as a final check .NET regex tool as it does not support .NET regex syntax.
@WiktorStribiżew, I kept OP pattern on the regex101 example
@WiktorStribiżew They are kept for the sake of using regex101. If they are not, regex101 will show no match.
@JJEdgarVI: I do not think you have to depend on / regex delimiters (not used in .NET) there - regex101.com/r/oB3tM7/2
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.