简体   繁体   中英

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. But I need only Guids, like:

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

[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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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