I’m trying to create a regex that reads a string, and if the last character is something like !”£$% etc, it ignores the last character, reads the string (to allow my code to look it up in a dictionary class) and then outputs the string, with the character on the end it ignored. Is this actually possible, or do I have to just remove the last character?
So far…
foreach(var line in yourReader)
{
var dict = new Dictionary<string,string>(); // your replacement dictionaries
foreach(var kvp in dict)
{
System.Text.RegularExpressions.Regex.Replace(line,"(\s|,|\.|:|\\t)" + kvp.Key + "(\s|,|\.|:|\\t)","\0" + kvp.Value + "\1");
}
}
I’ve also been told to try this
var trans = textbox1.Text;
foreach (var kvp in d) //d is my dictionary so use yours
{
trans = trans.Replace(kvp.Key, kvp.Value);
}
textbox2.Text = trans;
but have literally no idea what it does
I didn’t find any point using Regex, so I hope this will help:
Edit:
I checked the old code, and it had a problem: when the string’s last “forbidden” characters were in order of the
ForbiddenCharsarray it deleted all of them. if your string was “Hello World&!” it would delete both the ! and &. so I set abreak;and it won’t be a problem anymore.