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

これはあなたの正規表現です

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

<!inc(一致するものがある場合にのみ分割 (および削除) します)!>(逆も同様です)。

于 2012-05-22T07:34:31.973 に答える
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 に答える