4

これは簡単なはずです...Rubyでcssファイルの16進数を3つ続けて一致させようとしています。これが私が持っているものです..

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

これは、そのような値がいくつかあるファイルには何も返しません。行を に変更するとline =~ /\h/、事実上すべての行が返されます。基本的な何かが欠けているに違いないことはわかっていますが、それは何ですか?

編集。入力例を次に示します。もちろん、有効な 16 進数の色は 3 つの 16 進数値の組み合わせにすることができますが、今のところ、6 つの値を持つものだけに関心があります。

#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}
4

1 に答える 1

6

これはどうですか?

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

3文字または6文字が必要な場合は、このバリエーションで...

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

コンソールのテスト

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"]]
于 2013-04-01T02:54:20.237 に答える