/// <summary> /// Replaces all strings having the form \label{name_of_label} with consecutive elements in Numbers, all /// strings of the form \ref{name_of_label} with numbers corresponding to the name_of_label /// Strings of the form "\label{name_of_label}" are added to the list /// Fills properties "LabelsFull" and "ReferencesFull" /// </summary> public void Replace() { const string AnyToFind = @"(.*?)"; Regex LabelsRegex = new Regex(@"\\" + "label" + @"\{" + AnyToFind + @"\}"); //Regex LabelsRegex = new Regex((MyStringOperations.ReplaceParameters(LabelsPattern, AnyToFind))); //Regex ReferenceRegex = new Regex(MyStringOperations.ReplaceParameters(ReferencePattern, AnyToFind)); Regex ReferenceRegex = new Regex(Regex.Escape(@"\") + "ref" + Regex.Escape(@"{") + AnyToFind + Regex.Escape(@"}")); //Put into FoundLabels all labels found in the string Text MatchCollection FoundLabels = LabelsRegex.Matches(Text); const int StartNumber = 1; int Number = StartNumber; //Replace all labels with appropriate numbers foreach (Match Found in FoundLabels) { string LabelsString = MyStringOperations.ReplaceParameters(ReferencePattern, Found.Groups[1].ToString()); Regex LabelsInstance = new Regex(LabelsString); string LabelFull = MyStringOperations.ReplaceParameters(LabelsPattern, Found.Groups[1].ToString()); Text = LabelsInstance.Replace(Text, Number.ToString()); Numbers.Add(Number); if (Labels.Contains(Found.Groups[1].ToString())) { const string Duplicates = "Duplicate labels: "; MessageBox.Show(Duplicates + Found.Groups[1].ToString()); //throw new InvalidOperationException(Duplicates + Found.Groups[1].ToString()); } Labels.Add(Found.Groups[1].ToString()); LabelsFull.Add(@Found.ToString()); Number++; } MatchCollection FoundReferences = ReferenceRegex.Matches(Text); //Replace all references with appropriate numbers //Fill the properties: References and ReferencesFull foreach (Match Found in FoundReferences) { if (!References.Contains(Found.Groups[1].ToString())) { References.Add(Found.Groups[1].ToString()); ReferencesFull.Add(@Found.ToString()); } } foreach (string Reference in References) { if (!Labels.Contains(Reference)) { const string Unresolved = "Unresolved reference: "; MessageBox.Show(Unresolved + Reference); //throw new InvalidOperationException(Unresolved + Reference); } else { int ReferenceNumber = Labels.LastIndexOf(Reference) + 1; string InstanceString = MyStringOperations.ReplaceParameters(ReferencePattern, Reference); Regex Instance = new Regex(InstanceString); Text = Instance.Replace(Text, Numbers[ReferenceNumber - 1].ToString()); } } }
/// <summary> /// Returns the reference corresponding to the label Word. /// </summary> /// <param mainFile="Word">A label.</param> /// <returns>The reference corresponding to the label Word.</returns> public static string GiveReference(string Word) { return(MyStringOperations.ReplaceParameters(ReferencePattern, Word) + MyStringOperations.Space); }