tags:

views:

81

answers:

6

Hi, Can somebody help me to get a content between [%= and %].

+1  A: 

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

prodigitalson
A: 
(?<=\[%=).*?(?=%])

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();
} 
Tim Pietzcker
A: 
\[%=([^%]|%[^\]])*%\]

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.

Nate C-K
I like this one :D
Gumbo
A: 

Try this:

\[%=((?:[^%]|%[^\]])*)%]
Gumbo
Yeah, and this one looks pretty good too ;)
Nate C-K
@Nate C-K: Yeah, two idiots, one thought.
Gumbo
I got a question about a regular expression for C-style comments wrong on a test once, so now the solution is forever burned into my brain.
Nate C-K
+2  A: 

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.

Greg Bacon
No regular expression can handle nesting.
Nate C-K
@Nate - sure it can. recursive regex.
Tor Valamo
There is no such thing as a recursive regular expression (despite whatever misnomers people might come up with). If it is recursive then it is by definition not regular.
Nate C-K
@Tor: Although if by "regex" you mean "what Perl can do with those things that look like regular expressions" and you don't mean "regular expression" then you are of course correct.
Nate C-K
+2  A: 

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.

Mark Byers