3

I'm trying to figure out how I can match any .css filename, not starting with an underscore, preceding any string. I found a good starting point from this question on stackoverflow (ActiveAdmin assets precompile error) :

[/^[^_]\w+\.(css|css.scss)$/]

However, this regex only matches filename.css without an underscore. I'd like to have a regex that matches any path before the filename without underscore. The following strings should match :

mystyle.css
application.css.scss
/assets/stylesheets/application.css

but the following strings should not match :

_mystyle.css
_application.css.scss
/assets/stylesheets/_application.css

Any help would be appreciated.

3 Answers 3

2

Something like this should work:

/(.+\/|^)[a-z0-9\.]+\.s?css$/

Not all ruby versions support it, but you coul also try a negative lookahead:

/.+\/?(?!_)\w+\.s?css$/

http://ruby-doc.org/core-2.0/Regexp.html#label-Anchors

Sign up to request clarification or add additional context in comments.

2 Comments

the negative lookahead didn't work for me. The other one does! Thank u.
The second version is incorrect. The lookahead fails because the previous .+ can just match past it if it wants to - the slash is optional. And the first version doesn't allow an underscore (or an uppercase letter) anywhere in the filename...
2

This should work:

/^(.*?\/)?[^_]\w*\.(css|css\.scss)$/

Explanation:

(.*?\/)?  # Means it accepts any characters upfront, ending with a slash, 
          # then the filename. The ? makes it optional.

4 Comments

Reading through and understanding regular-expressions.info/tutorial.html is a great start. You can also use online tools to test regex such as regex101.com while reading the tutorial, which will also tell you what each part of a regex does.
You should mark an answer as accepted if it solved your problem, to help others find the correct answer if they google your question.
Note that this regex will fail to match filenames that are only one character in length (like a.css).
@TimPietzcker You are, as always, correct. I shamelessly copied the rest of the regex. I'll update the answer.
1

Assuming that the input is always a file path, I would prefer to do like this:

File.basename(file_path).match(/_.*(css|scss|sass)/)

Regexps are hard to read, and so to improve your code readability is a good idea to use as few as necessary.

Also, if you are doing other matches, you might want to extract the css file extensions in other regexp like

css_exts_regexp = /(css|scss|sass)/
 File.basename(file_path).match(/_.*#{css_exts_regxp}/)

2 Comments

Depending on whether this is a valid option, this is a better solution than any regex.
I can't really use this solution because the file_path is an unknown factor in my case. I have to provide the regex in this form : config.assets.precompile += [/^[^_]\w+\.(css|css.scss)$/] .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.