简体   繁体   中英

How to match '{{{' using RegEx

The given string is something like:

words words {{{value}}} some other words {{{value2}}}

I'm trying the following one:

const string pattern = @"\b({{{)\w+(}}})\b";
Regex optionRegex = new Regex(pattern, RegexOptions.Compiled);
MatchCollection matches = optionRegex.Matches(text);

and @"\\b(\\{\\{\\{)\\w+(\\}\\}\\})\\b" didn't helped. Please, help to create regex, TIA

Your regex should be:

@"{{{\w+}}}"

The problem is that there is no word boundary ( \\b ) where you were trying to match.

You can add the grouping in if you need it, but it seems unlikely that you do since you know that the first group contains {{{ and the second contains }}} . Perhaps you meant to group the word inside:

@"{{{(\w+)}}}"

删除\\b ,它意味着单词边界,但是在给定字符串中,空格和{之间没有任何关系。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM