简体   繁体   English

所需标签的正则表达式模式

[英]regex pattern for tags needed

Howzit, z

I need help with the following please. 我需要以下帮助。

I need to find tags in a string. 我需要在字符串中查找标签。 These tags start with {{ and end with }} , there will be multiple tags in the string I receive. 这些标签以{{开始,以}}结尾,我收到的字符串中将有多个标签。

So far I have this, but it doesn't find any matches, what am I missing here? 到目前为止,我有这个,但是找不到任何匹配项,我在这里错过了什么?

List<string> list = new List<string>();

string pattern = "{{*}}";

Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
Match m = r.Match(text);

while (m.Success)
{
    list.Add(m.Groups[0].Value);

    m = m.NextMatch();
}


return list;

even tried string pattern = "{{[A-Za-z0-9]}}"; 甚至尝试过的string pattern = "{{[A-Za-z0-9]}}"; thanx 谢谢

PS. PS。 I know close to nothing about regex. 我对正则表达式几乎一无所知。

Not only do you want to use {{.+?}} as your regex, you also need to pass RegexOptions.SingleLine. 您不仅要使用{{。+?}}作为正则表达式,还需要传递RegexOptions.SingleLine。 That will treat your entire string as a single line and the . 这会将您的整个字符串视为一行,而。 will match \\n (which it normally will not do). 将匹配\\ n(通常不匹配)。

Try {{.+}} . 尝试{{.+}} The .+ means there has to be at least one character as part of the tag. .+表示标签中必须至少包含一个字符。

EDIT: 编辑:

To capture the string containing your tags you can do {{(.+)}} and then tokenize your match with the Tokenize or Scanner class? 要捕获包含标签的字符串,您可以执行{{(.+)}} ,然后使用Tokenize或Scanner类对您的匹配进行标记化?

I would recommend trying something like the following: 我建议您尝试以下操作:

List<string> list = new List<string>();

string pattern = "{{(.*?)}}";

Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
Match m = r.Match(text);

while (m.Success)
{
    list.Add(m.Groups[1].Value);

    m = m.NextMatch();
}

return list;

the regex specifies: 正则表达式指定:

{{       # match {{ literally
(        # begin capturing into group #1
  .*?    # match any characters, from zero to infinite, but be lazy*
)        # end capturing group
}}       # match }} literally

"lazy" means to attempt to continue matching the pattern afterwards " }} " before backtracking to the .*? “懒惰”是指在回溯到.*?之前,尝试在“ }} ”之后继续匹配模式.*? and reluctantly adding a character to the capturing group only if the character does not match }} - hope that made sense. 并且在字符不匹配时才勉强将字符添加到捕获组中}} -希望这样做有意义。

I changed your code by modifying the regex and to extract the first matching group from the regex match object ( m.Groups[1].value ) instead of the entire match. 我通过修改正则表达式并从正则表达式匹配对象( m.Groups[1].value )而不是整个匹配中提取第一个匹配组来更改了代码。

{{.*?}} or {{.+?}} {{.*?}}{{.+?}}

. - means any symbol -表示任何符号

? - means lazy(don't capute nextpattern) -表示懒惰(不要模仿nextpattern)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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