简体   繁体   English

C#Regex.Split和正则表达式

[英]C# Regex.Split and Regular expression

I have string, I need split it two times and select part which goes after special character. 我有字符串,我需要将其拆分两次,然后选择要放在特殊字符后面的部分。

Lets say: 可以说:

string myString = "Word 2010|82e146e7-bc85-4bd4-a691-23d55c686f4b;#Videos|55140947-00d0-4d75-9b5c-00d8d5ab8436";

string[] guids = Regex.Split(myString,";#");

So here I am getting array of two elements with Value + GUID. 所以在这里,我得到了两个带有Value + GUID元素的数组。 But I need only Guids, like: 但我只需要Guid,例如:

[0]82e146e7-bc85-4bd4-a691-23d55c686f4b [0] 82e146e7-bc85-4bd4-a691-23d55c686f4b

[1]55140947-00d0-4d75-9b5c-00d8d5ab8436 [1] 55140947-00d0-4d75-9b5c-00d8d5ab8436

Any way of doing it in one/two lines? 用一两行来做吗?

You can do this but just because you can do it in one line doesn't mean you should (readability comes into play if you get too fancy here). 您可以做到这一点,但是仅仅因为您可以一行完成就可以了 ,但这并不意味着您应该这样做 (如果您太喜欢这里,便会提高可读性)。 There's obviously no validation here at all. 显然这里根本没有验证。

string myString = "Word 2010|82e146e7-bc85-4bd4-a691-23d55c686f4b;#Videos|55140947-00d0-4d75-9b5c-00d8d5ab8436";

string[] guids = Regex.Split(myString, ";#")
                      .SelectMany(s => Regex.Split(s, @"\|").Skip(1))
                      .ToArray();

Assert.AreEqual(2, guids.Length);
Assert.AreEqual("82e146e7-bc85-4bd4-a691-23d55c686f4b", guids[0]);
Assert.AreEqual("55140947-00d0-4d75-9b5c-00d8d5ab8436", guids[1]);

如果每个输入的最后部分始终是guid,则可以轻松地执行此操作而无需使用正则表达式:

string[] guids = String.Split(";").Select(c => c.Substring(c.Length - 36)).ToArray();
string[] guids = myString.Split(';').Select(x => x.Split('|')[1]).ToArray();
string myString = "Word 2010|82e146e7-bc85-4bd4-a691-23d55c686f4b;#Videos|55140947-00d0-4d75-9b5c-00d8d5ab8436";

//split the string by ";#"
string[] results = myString.Split(new string[] { ";#" }, StringSplitOptions.RemoveEmptyEntries);

//remove the "value|" part
results[0] = results[0].Substring(results[0].IndexOf('|') + 1);
results[1] = results[1].Substring(results[1].IndexOf('|') + 1);

//Same as above, but in a for loop. usefull if there are more then 2 guids to find
//for(int i = 0; i < results.Length; i++)
//  results[i] = results[i].Substring(results[i].IndexOf('|') + 1);

foreach(string result in results)
    Console.WriteLine(result);
var guids = Regex
    .Matches(myString, @"HEX{8}-HEX{4}-HEX{4}-HEX{4}-HEX{12}".Replace("HEX", "[A-Fa-f0-9]"))
    .Cast<Match>()
    .Select(m => m.Value)
    .ToArray();

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

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