Regex匹配CSS颜色十六进制码。

时间:2021-01-23 00:10:02

This should be easy...I'm trying to match 3 hex digits in a row in a css file with ruby. Here's what I've got..

这应该很容易…我正在尝试用ruby在css文件中的一行中匹配3个十六进制数字。这就是我. .

File.open(ARGV[0], 'r') do |source|
  source.each { |line|
  puts line if line =~ /\h{3}/
}
end

This doesn't return anything back in a file that has several of such values. if I change line to be line =~ /\h/ then virtually every line gets returned. I know I must be missing something basic but what is it?

这不会返回文件中有几个这样值的任何内容。如果我将行改为line =~ /\h/,那么几乎每一行都会返回。我知道我一定遗漏了一些基本的东西,但那是什么呢?

EDIT. Here's some sample input. Valid hex colors of course can be three hex value combinations, but for now im just concerned with the six valued ones.

编辑。下面是一些示例输入。有效的十六进制颜色当然可以是三个十六进制值组合,但现在我只关心六个值。

#captcha fieldset{border-top:1px solid #c0c0c0;border-bottom:1px solid#c0c0c0;margin:0;padding:10px}
#captcha legend{color:gray}
#captcha .divider{display:none}
#captcha .captcha_refresh{font-size: 9px;color:gray}
#captcha .captcha_other_options{padding-top:5px;font-size: 9px}
#captcha .recaptcha_text{font-size: 11px;line-height:16px}
#captcha .captcha_optout{font-size: 11px;padding:10px 0 5px}
#captcha #recaptcha_image{font-weight:bold;margin:10px 0 0 0}
#captcha #recaptcha_image a.recaptcha_audio_cant_hear_link{font-size: 9px;font-weight:normal}
#captcha .captcha_loading{border:0}
#captcha .captcha_image img{border:1px solid #c0c0c0}
#captcha .captcha_input input{direction:ltr;margin-top:4px;width:137px}
#captcha .captcha_input label{margin-right:4px}
.register #captcha .captcha_input label{color:#666;font-weight:bold}
#generic_dialog.captcha .generic_dialog_popup{width:340px}

1 个解决方案

#1


5  

What about this one?

这一个怎么样?

/(?<=#)(?<!^)\h{3}/

With this variation if you want 3 or 6 characters...

如果你想要3个或6个字符…

/(?<=#)(?<!^)(\h{6}|\h{3})/

Console testing

1.9.3p392 :002 > css = "#captcha fieldset{border-top:1px solid #c0c0c0;border-bottom:1px solid#c0c0c0;margin:0;padding:10px}"
 => "#captcha fieldset{border-top:1px solid #c0c0c0;border-bottom:1px solid#c0c0c0;margin:0;padding:10px}"
1.9.3p392 :003 > css.scan(/(?<=#)(?<!^)(\h{6}|\h{3})/)
 => [["c0c0c0"], ["c0c0c0"]]

#1


5  

What about this one?

这一个怎么样?

/(?<=#)(?<!^)\h{3}/

With this variation if you want 3 or 6 characters...

如果你想要3个或6个字符…

/(?<=#)(?<!^)(\h{6}|\h{3})/

Console testing

1.9.3p392 :002 > css = "#captcha fieldset{border-top:1px solid #c0c0c0;border-bottom:1px solid#c0c0c0;margin:0;padding:10px}"
 => "#captcha fieldset{border-top:1px solid #c0c0c0;border-bottom:1px solid#c0c0c0;margin:0;padding:10px}"
1.9.3p392 :003 > css.scan(/(?<=#)(?<!^)(\h{6}|\h{3})/)
 => [["c0c0c0"], ["c0c0c0"]]