更换{tag}的一个值或完全删除{任何标签}标签、tag

2023-09-06 06:07:21 作者:浊酒一杯温如言

我已经设计了一个多语种网站,并在数据库中的一些价值观有,它会代替它的语言值标记,删除标签括号(如遇{})或完全删除。

I've designed a multilingual web site and some values in database have a tag which will be replaced with it's language value, remove tags brackets ( in case {} ) or removed completely.

有两种情况:

删除括号:

value {mm} >> value mm

删除完全:

value {mm} >> value

还有{}标签可以是任意长度,可包含 -

Also {tag} could be any length and can contain -

任何人可以帮助我与正则表达式?

Can anybody help me with regex?

推荐答案

下面是一些code,你可能会发现有用。对于许多的许多的更多选项,请参见定期-EX pressions.info 。

Here is some code you might find useful. For many many more options, see Regular-expressions.info.

所有code 使用System.Text.RegularEx pressions;

删除所有出现的 {毫米} (和仅的毫米):

Remove all occurrences of {mm} (and only mm):

Regex.Replace(myString, "{mm}", String.Empty, RegexOptions.None);

替换 {毫米} (和仅的毫米)使用毫米:

Replace all occurrences of {mm} (and only mm) with mm:

Regex.Replace(myString, "{mm}", "mm", RegexOptions.None);

删除的所有实例{任何字符}

Regex.Replace(myString, @"{[\-a-z]*}", String.Empty, RegexOptions.IgnoreCase);

替换的所有实例{任何字符} 的任何字符

Regex.Replace(myString, @"{(?<tag>[\-a-z]*)}", "${tag}", RegexOptions.IgnoreCase);