0

相关: https ://stackoverflow.com/a/2910549/194031

我有一个像这样的字符串:

"abc defgh <!inc(C:\my files\abc.txt)!>kdi kdkd<!inc(C:\my files\abc.txt)!>"

我想得到:

["abc defgh ", "C:\my files\abc.txt", "kdi kdkd", "C:\my files\abc.txt"]

还有,我不想

"abc <!inc(C:\my files\abc.txt adf" (missing end bracket) 

分裂。

基于相关的问题和其他类似的答案,我需要使用前瞻,但我无法弄清楚如何在完成删除标签并且如果部分标签丢失时不拆分时使用它们。

4

2 回答 2

2

这可能会帮助您入门。您可能需要对其进行更多调整。

Regex.Split("...", @"<!inc\((?=.*?\)!>)|(?<=<!inc\(.*?)\)!>");

表达式分解

<!inc\(
(?=.*?\)!>)    // this is the positive lookahead to make sure that the string ')!>`
               // exists before counting this as a match
|
(?<=<!inc\(.*?) // positive look behind to make sure '<!inc(' shows up before
\)!>         
于 2012-05-22T07:22:15.720 回答
2

这是你的正则表达式

<!inc\((?=[^)]+\)!>)|(?<=<!inc\([^)]+)\)!>

<!inc(当且仅当它具有匹配项时,它才会拆分(并删除))!>(反之亦然)。

于 2012-05-22T07:34:31.973 回答