Regular expression help: how to ignore every path that isn’t a CSS file

578 views Asked by At

I have a CSS framework submodule in my Git repo that includes a bunch of README, component.json and other files. I don’t want to modify or delete the files because I’d imagine it’d cause problems when updates are pushed to the submodule. Yet Middleman wants to process them.

I currently have this in my config.rb file:

# Ignore everything that's not a CSS file inside inuit.css
ignore 'css/inuit.css/*.html'
ignore 'css/inuit.css/*.json'
ignore 'css/inuit.css/LICENSE'

How could I express this with a file pattern or a regex?

2

There are 2 answers

1
jaeheung On BEST ANSWER

I’m not familiar with Middleman, but doesn’t this work?

ignore /^css\/inuit\.css\/.*(?<![.]css)$/
0
Rory O'Kane On

Since ignore can take a regex, pass ignore a Ruby regex // instead of a string "" with a filename glob. In the regex, use negative lookahead (?!) and the end-of-string anchor $ to check that the filename doesn’t end in “.css”.

ignore /^ css\/inuit\.css\/ (?:  [^.]+  |  .+ \. (?!css) \w+   ) $/ix

This regex correctly handles all of these test cases:

  • Should match:
    • css/inuit.css/abc.html
    • css/inuit.css/thecssthing.json
    • css/inuit.css/sub/in_a_folder.html
    • css/inuit.css/sub/crazily.named.css.json
    • css/inuit.css/sub/crazily.css.named.json
    • css/inuit.css/LICENSE
  • Shouldn’t match:
    • css/inuit.css/realcss.css
    • css/inuit.css/main.css
    • css/inuit.css/sub/in_a_folder.css
    • css/inuit.css/sub/crazily.css.named.css
    • css/inuit.css/sub/crazily.named.css.css

The first alternation of the (?:) non-capturing group handles the case of files with no extension (no “.”). Otherwise, the second case checks that the last “.” in the path is not followed by “css”, which would indicate a “.css” extension.

I use the x flag to ignore whitespace in the regex, so that I can add spaces in the regex to make it clearer.