开发者

Regex: get content between [%= and %]

开发者 https://www.devze.com 2022-12-18 02:16 出处:网络
Can somebody help me to get a content between [%= an开发者_StackOverflow中文版d %].If there cannot be nested tags you can use the following regex:

Can somebody help me to get a content between [%= an开发者_StackOverflow中文版d %].


If there cannot be nested tags you can use the following regex:

\[%=(.*?)%]

The symbols mean the following:

\[    Match a literal [ character. The backslash is required otherwise [ would
      start a character class.
%=    Match %=
(.*?) Match any characters, non-greedy. i.e. as few as possible. The parentheses
      capture the match so that you can refer to it later.
%]    Match %] - Note that it is not necessary to escape ] here, but you can if
      you want.

Here's how you could use it in C#:

string s = "sanfdsg[%=jdgashg%]jagsklasg";
Match match = Regex.Match(s, @"\[%=(.*?)%]");
if (match.Success)
{
    Console.WriteLine(match.Groups[1].Value);
}

Output:

jdgashg

Or to get multiple matches:

string s = "foo[%=bar%]baz[%=qux%]quux";
foreach (Match match in Regex.Matches(s, @"\[%=(.*?)%]"))
{
    Console.WriteLine(match.Groups[1].Value);
}

Output:

bar
qux

Note the string literal is written as @"...". This means that the backslashes inside the string are treated as literal backslashes, and not escape codes. This is often useful when writing regular expressions in C#, to avoid having to double up all the backslashes inside the string. Here it doesn't make much difference, but in more complex examples it will help more.


You could use simple

\[%=(.*?)%\]

but you should realize that it won't handle nesting correctly. If the content may span multiple lines, you'll also need to specify RegexOption.Singleline to make .*? cross line boundaries.


%=\s?(.*?)\s?% perhaps?


Try this:

\[%=((?:[^%]|%[^\]])*)%]


\[%=([^%]|%[^\]])*%\]

This doesn't rely on any of the greediness operators and thus should translate to any regular expression language. You may or may not care about that.


(?<=\[%=).*?(?=%])

will match any text (including linebreaks) between these two delimiters (without matching the delimiters themselves). Nested delimiters are not supported.

To iterate over all matches:

Regex my_re = new Regex(@"(?<=\[%=).*?(?=%\])", RegexOptions.Singleline);
Match matchResults = my_re.Match(subjectString);
while (matchResults.Success) {
    // matched text: matchResults.Value
    // match start: matchResults.Index
    // match length: matchResults.Length
    matchResults = matchResults.NextMatch();
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号