繁体   English   中英

使用RegEx在特定字符后分割字符串

[英]Using RegEx to split strings after specific character

我一直在尝试将此字符串拆分到我设法使用的几个不同地方,除非该名称中包含正斜杠,否则它将完全使所有组分离。

字符串:

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

我基本上需要以下内容:

  • IP组:123.45.678.90:00000(不含/)
  • id组:98765432109876541
  • 名称群组:[CLAN] PlayerName
  • id1群组:12345678901234567

文本“ joined”也必须存在。 但是,windows没有。

这是我到目前为止的内容:

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

除非玩家名称中包含“ /”, 否则这就像一个咒语。 我将如何逃脱呢?

任何帮助,将不胜感激!

由于您使用C#Regex标记了问题,而不仅是Regex ,我将提出一个替代方案。 我不确定它是否会更有效。 如果您只使用String.Split()我发现它最容易阅读和调试:

演示版

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);
}

输出:

[CLAN]Player/Na/me joined

如果您正在使用ip,groupId等的类,而您确实这样做了,则只需使用接受字符串作为参数的构造函数将所有内容放入其中。

基本上,你需要使用非贪婪的选择( *? 尝试这个:

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

您不应该使用带有开放字符(例如)的贪婪量词( *. 它无法按预期工作,并且会导致大量回溯。

这会稍微提高效率,但又不过分严格:

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

正则表达式演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM