Hello this is a Console App(.Net Core) in Visual Studio 2019 and i am trying to split a string into sentences and then add them into a dictionary with Key the first word of the sentence and Value the sentence itself. I have the following code and i wonder if the way i did it has of course a better way to handle it.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace StringParse
{
class Program
{
static void Main(string[] args)
{
string phrase = @"MHD001|LOCCOD|GRCACS|20191117|133601|CB|trx_grcacs_tsk_20191117133601498|
TSK001|GLS|GRCACS|59144|59144|2|TU|93000161357|TRACE|INF|DE 800|please send us the completely address information-thanks|68481|20191117|133555|
TSA001|X|
TSI001|3655|please send us the completely address information-thanks|66510|
TSI001|5637|80337|61335|
TSI001|5841|DE|61313|
MTR001|5|";
/////////////////////////////////////////////////////////////////////
string sentenceSeparator = @"\b[A-Z]{3}\d{3}\b";
string[] words = phrase.Split('|');
StringBuilder sb = new StringBuilder();
for (int i = 0; i < words.Length; i++)
{
//If current word array matches the regex
if (words[i].Equals(sentenceSeparator))
{
// If the current array value doesn't match, then continue the current line.
sb.Append(words[i]);
sb.Append(" ");
}
else
{
if (i == 0)
{
// on the 1st iteration, we don't need a new line before the text value.
sb.Append(words[i]);
sb.Append(" ");
}
else
{
sb.Append(words[i]);
sb.Append(" ");
}
}
}
string[] sentences = sb.ToString().Split('\r');
////////////////////////////////////////////////////////////////////////
Regex regx = new Regex(sentenceSeparator);
MatchCollection matches = Regex.
Matches(phrase, sentenceSeparator,
RegexOptions.Compiled |
RegexOptions.CultureInvariant);
for (int i = 1; i < matches.Count; ++i)
{
phrase = phrase
.Replace(matches[i].Value,
Environment.NewLine + matches[i].Value);
}
///////////////////////////////////////////////////////////////////////////////
Dictionary<Match, string> dic = matches.Zip(sentences, (x, y)
=> new { Key = x, Value = y }).ToDictionary(x => x.Key, y => y.Value);
foreach (KeyValuePair<Match, string> kvp in dic)
{
Console.WriteLine("Key = {0}" + Environment.NewLine + "Value = {1}", kvp.Key, kvp.Value);
}
}
}
}
output will be like that:
Key = MHD001
Value = MHD001 LOCCOD GRCACS 20191117 133601 CB trx_grcacs_tsk_20191117133601498
Key = TSK001
Value = TSK001 GLS GRCACS 59144 59144 2 TU 93000161357 TRACE INF DE 800 please send us the completely address information-thanks 68481 20191117 133555
Key = TSA001
Value = TSA001 X
Key = TSI001
Value = TSI001 3655 please send us the completely address information-thanks 66510
Key = TSI001
Value = TSI001 5637 80337 61335
Key = TSI001
Value = TSI001 5841 DE 61313
Key = MTR001
Value = MTR001 5
if..else..if..else
seems senseless because in each branch the code is doing the exactly same thing. \$\endgroup\$MatchCollection
doesn't contain aZip
method. \$\endgroup\$Cast<Match>()
and it works ``` Dictionary<Match, string> dic = matches.Cast<Match>().Zip(sentences, (x, y) => new { Key = x, Value = y }).ToDictionary(x => x.Key, y => y.Value); ``` \$\endgroup\$