如果文本没有被方括号括起来,我必须解析一个带有关键字的文本.我要与关键字匹配带.此外,必须有在两侧字边界带.
下面是一些例子,其中有不是关键字:
以下是使用 IS关键字的一些示例
有人帮忙吗?提前致谢.
Tim*_*ker 17
您可以查找该单词with
并查看其左侧最近的支架不是左括号,并且右侧最近的支架不是右括号:
Regex regexObj = new Regex(
@"(?<! # Assert that we can't match this before the current position:
\[ # An opening bracket
[^[\]]* # followed by any other characters except brackets.
) # End of lookbehind.
\bwith\b # Match ""with"".
(?! # Assert that we can't match this after the current position:
[^[\]]* # Any text except brackets
\] # followed by a closing bracket.
) # End of lookahead.",
RegexOptions.IgnorePatternWhitespace);
Match matchResults = regexObj.Match(subjectString);
while (matchResults.Success) {
// matched text: matchResults.Value
// match start: matchResults.Index
// match length: matchResults.Length
matchResults = matchResults.NextMatch();
}
Run Code Online (Sandbox Code Playgroud)
环视表达式不会在换行符处停止; 如果您希望单独评估每一行,请使用[^[\]\r\n]*
而不是 [^[\]]*
.