开发者

Regular expression show results if expression doesn't match

开发者 https://www.devze.com 2023-03-23 17:03 出处:网络
Seemingly simple task. I need to display a line if a match is not found. Or, said another way, I need to not display a line if a match is found. I suspect this involves a look ahead assertion of some

Seemingly simple task. I need to display a line if a match is not found. Or, said another way, I need to not display a line if a match is found. I suspect this involves a look ahead assertion of some kind but can't figure out how.

I've tried finding a match and then trying to negate it with ^. But that doesn't seem to work. Also tried finding a matc开发者_开发知识库h and putting {0} next to it: (wordhere){0}


You can indeed use a negative lookahead. E.g.

^(?!something$).*$

will match anything besides "something".

Note that it may however be much more appropriate and better to read if you do a positive match and use control structures like if not ... to define which line is printed and which line is not.


If you can use grep,

grep -v <expr> 

does what you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消