[英]Using RegEx to split strings after specific character
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: 我基本上需要以下内容:
The text "joined" also has to be there. 文本“ joined”也必须存在。 However windows does not.
但是,windows没有。
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! 任何帮助,将不胜感激!
Since you tag your question with C#
and Regex
and not only Regex
, I will propose an alternative. 由于您使用
C#
和Regex
标记了问题,而不仅是Regex
,我将提出一个替代方案。 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()
: 如果您只使用
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);
}
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. 如果您正在使用ip,groupId等的类,而您确实这样做了,则只需使用接受字符串作为参数的构造函数将所有内容放入其中。
You basically needs to use non greedy selectors ( *?
). 基本上,你需要使用非贪婪的选择(
*?
Try this: 尝试这个:
(?<ip>.*?)\/(?<id>.*?)\/(?<name1>.*?)( joined )\[(.*?\/)?(?<id1>.*?)\]
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+)]$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.