gpt4 book ai didi

ruby-on-rails - 正则表达式忽略以单词开头的路径或以下划线开头的文件

转载 作者:数据小太阳 更新时间:2023-10-29 08:51:43 26 4
gpt4 key购买 nike

我一直在做一个 Ruby on Rails 项目,它有贪婪的 Assets 预编译正则表达式(这对我来说是可取的,因为我不包括):

# in config/application.rb
# this excludes all files which start with an '_' character (sass)
config.assets.precompile << /(?<!rails_admin)(^[^_\/]|\/[^_])([^\/])*.s?css$/

在同一个项目中,我使用 rails_admin插入。我需要贪婪的正则表达式来忽略 rails_admin Assets 。我开始玩弄一些 regex on Rubular但无法丢弃最后三个示例(rails_admin 开头的任何内容)。

我如何使用忽略所有 rails_admin Assets 和文件名以 _ 开头的 Assets 的正则表达式,但仍然抓取其他所有内容?

最佳答案

%r{               # Use %r{} instead of /…/ so we don't have to escape slashes
\A # Make sure we start at the front of the string
(?!rails_admin) # NOW ensure we can't see rails_admin from here
([^_/]|/[^_]) # (I have no idea what your logic is here)
([^/]*) # the file name
\.s?css # the extension
\z # Finish with the very end of the string
}x # Extended mode allows us to put spaces and comments in here

请注意,在 Ruby 中,正则表达式 ^$ 匹配 line 的开始/结束,而不是字符串,因此通常最好使用 \A\z 代替。


编辑:这是一个允许任何路径的修改版本:

%r{               # Use %r{} instead of /…/ so we don't have to escape slashes
\A # Make sure we start at the front of the string
(?!rails_admin) # NOW ensure we can't see rails_admin from here
(.+/)? # Anything up to and including a slash, optionally
([^/]*) # the file name
\.s?css # the extension
\z # Finish with the very end of the string
}x # Extended mode allows us to put spaces and comments in here

根据您的编辑和评论,这是一个匹配的正则表达式:

  • 任何以 .css 或 .scss 结尾的文件
  • 但如果路径以 rails_admin 开头则不是
  • 如果文件名以下划线开头则不是

演示:http://rubular.com/r/Y3Mn3c9Ioc

%r{               # Use %r{} instead of /…/ so we don't have to escape slashes
\A # Make sure we start at the front of the string
(?!rails_admin) # NOW ensure we can't see rails_admin from here
(?:.+/)? # Anything up to and including a slash, optionally (not saved)
(?!_) # Make sure that we can't see an underscore immediately ahead
([^/]*) # the file name, captured
\.s?css # the extension
\z # Finish with the very end of the string
}x # Extended mode allows us to put spaces and comments in here

关于ruby-on-rails - 正则表达式忽略以单词开头的路径或以下划线开头的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11104751/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com