3

我有一个简单的场景,似乎难倒我。我想获取两个未注释掉的标签之间的文本。这是一个例子:

// Example of commented text
// :Start
// <I don't want to get this text>
// :End


:Start
<Here is the text i want>
:End

解决方案:

感谢大家的帮助。超级快地收到答案,完全符合我的需要。我使用了以下正则表达式,因为它最适合我的情况。特别感谢 Tim Pietzcker:

(?sm)(?<=^:Start\s*)(?:(?!^:End).)*
4

3 回答 3

2

试试这个:

(?sm)(?<=^:Start\s*)(?:(?!^:End).)*

解释:

(?sm)     # Set options: . matches newline, ^ matches start-of line
(?<=      # Assert that this regex can be matched before the current position:
 ^        #  Start of line
 :Start   #  :Start
 \s*      #  Any whitespace
)         # End of lookahead
(?:       # Try to match...
 (?!      # (unless the following regex could be matched here:)
   ^      #  Start of line
   :End   #  :End
 )        #  End of lookahead
 .        # ... any character
)*        # Repeat any number of times
于 2012-10-09T17:37:44.603 回答
1

I would go for this, seems to be robust enough. Also catches multiple lines:

(?s)(?<=(?<!/+\s*):Start\s+)(?!//).+\s(?=:End)

(?s) at the begging for SingleLine option.

于 2012-10-09T18:07:58.133 回答
0

这种模式应该做到这一点。基本上,标签必须位于行首,以区分真实标签和注释标签。

"\n:Start\n([^\n\/]+)\n:End"

这是 Python 中的一个示例。s是您的示例文本。

r = re.search("\n:Start\n([^\n\/]+)\n:End", s)
r.group(1)
'<Here is the text i want>'

我不完全确定 .NET 中的语法,但看看这个我想它应该看起来像:

foreach (Match match in Regex.Matches(s, "\n:Start\n([^\n\/]+)\n:End"))
    Console.WriteLine("'{1}'), match.Groups[1].Value)
于 2012-10-09T17:26:55.763 回答