删除未使用的合并标签正则表达式

时间:2022-08-27 17:20:33

I am using C# and writing mail merge application. My users store the templates as follows

我正在使用C#并编写邮件合并应用程序。我的用户按如下方式存储模板

Dear [[-UserName-]],
You have been subscribed for [[-SubscriptionName-]]...

and so on. There will be lot of custom fields between [[-xxxxx-]] place holders. I am merging them fine. But sometimes they don't pass for some place holders. I would like to find those things using regular expressions and replace them with empty strings.

等等。 [[-xxxxx-]]占位符之间会有很多自定义字段。我把它们合并得很好。但有时他们不会为一些占位符而过。我想使用正则表达式找到那些东西并用空字符串替换它们。

Technically, I want to find out the regular expression to find [[-what ever it is in between-]] and replace with empty string along with [[--]] tags

从技术上讲,我想找出正则表达式来找[[ - 它之间是什么 - ]]并用空字符串替换[[ - ]]标签

2 个解决方案

#1


2  

You can use:

您可以使用:

\[{2}(-.*?-)\]{2}
# look for [[-, -]] and anything in between.

This is called a lazy dot star, see a demo on regex101.com.

这被称为懒星点,请参阅regex101.com上的演示。

#2


0  

Since there are no dashes inside the tag names, you can use this regex:

由于标记名称中没有破折号,因此您可以使用此正则表达式:

\[\[-([^-]+)-\]\]

The tag name will be in capture group 1. Obviously, I recommend using the @"" type of string for the regex.

标签名称将位于捕获组1中。显然,我建议使用@“”类型的字符串作为正则表达式。

You will want to use the replacement string:

您将需要使用替换字符串:

[[--]]

Essentially the regex finds [[-, then captures a bunch of non - characters, then finds -]].

本质上,正则表达式找[[ - 然后捕获一堆非字符,然后找到 - ]]。

#1


2  

You can use:

您可以使用:

\[{2}(-.*?-)\]{2}
# look for [[-, -]] and anything in between.

This is called a lazy dot star, see a demo on regex101.com.

这被称为懒星点,请参阅regex101.com上的演示。

#2


0  

Since there are no dashes inside the tag names, you can use this regex:

由于标记名称中没有破折号,因此您可以使用此正则表达式:

\[\[-([^-]+)-\]\]

The tag name will be in capture group 1. Obviously, I recommend using the @"" type of string for the regex.

标签名称将位于捕获组1中。显然,我建议使用@“”类型的字符串作为正则表达式。

You will want to use the replacement string:

您将需要使用替换字符串:

[[--]]

Essentially the regex finds [[-, then captures a bunch of non - characters, then finds -]].

本质上,正则表达式找[[ - 然后捕获一堆非字符,然后找到 - ]]。