Parsing a GUID from a string string

I have another option, as my GUIDS can be saved as a string string.

1. Accessibility|5102d73a-1b0b-4461-93cd-0c024738c19e
2. 5102d73a-1b0b-4461-93cd-0c024738c19e;#5102d73a-1b0b-4461-93cd-0c024733d52d
3. |;#5102d73a-1b0b-4461-93cd-0c024738c19e;#SharePointTag|5102d73a-1b0b-4461-93cd-0c024733d52d
3. Business pages|;#5102d73a-1b0b-4461-93cd-0c024738cz13;#SharePointTag|5102d73a-1b0b-4461-93cd-0c024733d52d

Could you guys help me out with ideas on how I can parse these tags and get a List Guides list at the end? Maybe regular expression can help in this situation?

+3
source share
4 answers

It looks like you are playing with managed metadata, a term storage identifier and a terminal set identifier :)

Just use regular regex (the "p" variable below):

string c1 = "Accessibility|5102d73a-1b0b-4461-93cd-0c024738c19e";
string c2 = "5102d73a-1b0b-4461-93cd-0c024738c19e;#5102d73a-1b0b-4461-93cd-0c024733d52d";
string c3 = "|;#5102d73a-1b0b-4461-93cd-0c024738c19e;#SharePointTag|5102d73a-1b0b-4461-93cd-0c024733d52d";
string c4 = "Business pages|;#5102d73a-1b0b-4461-93cd-0c024738cz13;#SharePointTag|5102d73a-1b0b-4461-93cd-0c024733d52d";
string p = @"([a-z0-9]{8}[-][a-z0-9]{4}[-][a-z0-9]{4}[-][a-z0-9]{4}[-][a-z0-9]{12})";

MatchCollection mc;

Console.WriteLine("#1");
mc = Regex.Matches(c1, p);
foreach (var id in mc)
    Console.WriteLine(id);

Console.WriteLine("#2");
mc = Regex.Matches(c2, p);
foreach (var id in mc)
    Console.WriteLine(id);

Console.WriteLine("#3");
mc = Regex.Matches(c3, p);
foreach (var id in mc)
    Console.WriteLine(id);

Console.WriteLine("#4");
mc = Regex.Matches(c4, p);
foreach (var id in mc)
    Console.WriteLine(id);

Which exit:

#1
5102d73a-1b0b-4461-93cd-0c024738c19e
#2
5102d73a-1b0b-4461-93cd-0c024738c19e
5102d73a-1b0b-4461-93cd-0c024733d52d
#3
5102d73a-1b0b-4461-93cd-0c024738c19e
5102d73a-1b0b-4461-93cd-0c024733d52d
#4
5102d73a-1b0b-4461-93cd-0c024738cz13
5102d73a-1b0b-4461-93cd-0c024733d52d
Press any key to continue...
+4
source
var possibleGuids = myString.Split("|;#".ToCharArray(), 
                                   StringSplitOptions.RemoveEmptyEntries);
Guid g;
foreach(var poss in possibleGuids)
{
  if(Guid.TryParse(poss, out g))
  {
      // g contains a guid!
  }
}
+4
source
string sContent = "your data"; // any of your four forms of input
string sPattern = @"([a-z0-9]*[-]){4}[a-z0-9]*";

MatchCollection mc = Regex.Matches(sContent, sPattern );

foreach (var sGUID in mc)
{
    // Do whatever with sGUID
}
+2

,

 "fist|second".Split('|')

, GUID, GUID

 Guid = new Guid(myString);

var guid = new Guid("Accessibility|5102d73a-1b0b-4461-93cd-0c024738c19e".Split("|")[1]);

var myArray = "5102d73a-1b0b-4461-93cd-0c024738c19e;#5102d73a-1b0b-4461-93cd-0c024733d52d".Split(';');
var guid1 = new Guid(myArray[0]);
var guid2 = new Guid(myArray[1].Replace('#',''));

, .

+1

All Articles