vendredi 29 décembre 2017

C# Replace "Whole words only" useing RegEx and dictionary

I would like to create code, witch are replacing words contained in one file, using another text file as dictionary (struct.: Key sep.:tab Value).

Current code:

                    var fileDictionary = new Dictionary<string, string>(File.ReadLines(dictionaryPath, Encoding.Default).Select(line => line.Split('    ')).ToDictionary(data => data[0], data => data[1]), StringComparer.InvariantCultureIgnoreCase);//create dictionary based on text file



                    for (int i = 0; i < rowNumber; i++)
                    {
                        var output = fileString[i].ToString();// current row, taked from other file
                        var replaced = Regex.Replace(output, String.Join("|", fileDictionary.Keys.Select(Regex.Escape)), m => fileDictionary[m.Value], RegexOptions.IgnoreCase);
                        var result = replaced.ToString();
                        outputFile += result.ToString();
                        outputFile += "\r\n";
                    }

For now everything works fine, I using RegEx to replace words collected in dictionary, but I have problem with replacing type "whole words only".

Decided to use pattern like @"\bsomeword\b" but when I implement it f.eg.:

                        var replaced = Regex.Replace(output, String.Join("|", String.Format(@"\b{0}\b", fileDictionary.Keys.Select(Regex.Escape))), m => fileDictionary[m.Value], RegexOptions.IgnoreCase);

code doesnt return any results. Final text file, looks like original file. Nothing happens. As I realize, problem is in dictionary key, when I am using pattern I actually change key and new one, does not exist in current dictionary. So if key does not occur, value is not replaced.

Have anybody any suggestions how to fix that? Or maybe somebody know some different way to replace whole words only, using RegEx and dictionary?

Aucun commentaire:

Enregistrer un commentaire