Ed's response showed a working pattern, since striping is not supported in Notepad ++, however the rest of your problem cannot be handled only with regular expression. What you are trying to do is not possible with the search / replace regular expression approach. Your desired result includes logical conditions that cannot be expressed in regular expression. All you can do with the replacement method is to reorder the elements and refer to the captured elements, but you cannot tell them to use βAβ for values ββ1-3 and βBβ for 4-6. In addition, you cannot designate such placeholders. They are really capture groups that you are re-contacting.
To achieve the results that you showed, you will need to write a small program that will allow you to check the values ββobtained and make the appropriate replacements.
EDIT: here is an example of how to achieve this in C #
var numToWordMap = new Dictionary<int, string>(); numToWordMap[1] = "A_One"; numToWordMap[2] = "A_Two"; numToWordMap[3] = "A_Three"; numToWordMap[4] = "B_One"; numToWordMap[5] = "B_Two"; numToWordMap[6] = "B_Three"; string pattern = @"\bMyLabel_(\d+)\b"; string filePath = @"C:\temp.txt"; string[] contents = File.ReadAllLines(filePath); for (int i = 0; i < contents.Length; i++) { contents[i] = Regex.Replace(contents[i], pattern, m => { int num = int.Parse(m.Groups[1].Value); if (numToWordMap.ContainsKey(num)) { return "Label_" + numToWordMap[num]; }
You should be able to use it easily. Perhaps you can download LINQPad or Visual C # Express to do this.
If your files are too large, this may be an inefficient approach, in which case you can use StreamReader and StreamWriter to read from the source file and write it to another, respectively.
Also remember that my sample code writes back to the source file. For testing purposes, you can change this path to another file so that it is not overwritten.
source share