Matching everything but a specific word
I've got this regex:
.*js/[\bpackaged\b]+\.js
At the moment it matches 'packaged.js' from the list below:
- js/foo.js
- js/bar.js
- js/suitcase.js
- js/bootstrap.js
- js/packaged.js
How can I invert this so that it matches all the others except开发者_运维技巧 for 'packaged.js'?
kent$ echo " js/foo.js
js/bar.js
js/suitcase.js
js/bootstrap.js
js/packaged.js
"|grep -P '.*js/.*(?<!packaged).js'
js/foo.js
js/bar.js
js/suitcase.js
js/bootstrap.js
the example is just showing the expression, if you really use grep, -v could make it easier.
--- updated based on glibdud's comment ---
notice the last 5 lines in input:
kent$ echo " js/foo.js
js/bar.js
js/suitcase.js
js/bootstrap.js
js/packaged.js
js/foo_packaged.js
js/packaged_bar.js
js/foo_packaged_bar.js
js/_packaged.js
js/packaged_.js"|grep -P '.*js/(.+packaged\.js|.+(?<!packaged)\.js)'
output:
js/foo.js
js/bar.js
js/suitcase.js
js/bootstrap.js
js/foo_packaged.js
js/packaged_bar.js
js/foo_packaged_bar.js
js/_packaged.js
js/packaged_.js
This does what you want:
.*js/(?!packaged\.js).+\.js
精彩评论