Ahmed Nuaman
Ahmed Nuaman

Reputation: 13221

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:

How can I invert this so that it matches all the others except for 'packaged.js'?

Upvotes: 2

Views: 155

Answers (2)

Kent
Kent

Reputation: 195039

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

Upvotes: 3

nobody
nobody

Reputation: 10635

This does what you want:

.*js/(?!packaged\.js).+\.js

Upvotes: 0

Related Questions