我有一个字符串:
/* here is text */
here is text
我的正则表达式:(?!\/\*.+?\*\/).+?
结果:
/ *这里是文字* /
此处为文字
如何忽略/* here is text */
并获取不带/* here is text */
的字符串?
答案 0 :(得分:2)
你似乎误解了negative lookahead assertions。它们在正则表达式匹配期间不会跳过任何文本,它们只是断言某个正则表达式无法在当前位置匹配。
在您的情况下,这意味着your regex will match every single character (one for each time you perform the match operation) after the initial /
。你不希望这样。
使用正则表达式\/\*.+?\*\/\s*
匹配所有评论;如果评论可能跨越多行,则需要设置正则表达式引擎的DOTALL
或Singleline
标记。然后用空字符串替换那些匹配项。