I want to create an application for a mailing (not digital).
The idea is that the user creates a template in word (saved under RTF, already gave up on .doc).
Heres an example of template (in rtf):
“Happy Birthday [Username], …..”
Replacing the [Username] field with a specific text is easy. What I want to do however is find all strings beginning with ‘[‘ and ending with ‘]’ effectively. So I can give the users of my app the ability to chose which databasefield goes where.
This is what I got so far:
System.Windows.Forms.RichTextBox rtBox = new System.Windows.Forms.RichTextBox();
string s = System.IO.File.ReadAllText(path);
Regex regex = new Regex(@"\[.*\] ");
rtBox.Rtf = s;
var Matches = regex.Matches(rtBox.Rtf);
int i = 0;
while(i < Matches.Count) {
MessageBox.Show(Matches[i].ToString());
i++;
}
This code however doesn’t work as expected, if 2 parameters would be on 1 line (eg: “Happy bday [FirstName] [LastName]” the regex match is [*FirstName][LastName]*, not 2 separate.
Thanks!
Use
@"\[.*?\]"as pattern