4

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..

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?

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}
2
  • Can we see some sample input? \h will match [a-fA-F\d] so you know. Commented Apr 1, 2013 at 2:40
  • added. That's part of facebooks css I think... Commented Apr 1, 2013 at 2:45

1 Answer 1

6

What about this one?

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

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

/(?<=#)(?<!^)(\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"]]
Sign up to request clarification or add additional context in comments.

6 Comments

css-colors.rb:19: undefined (?...) sequence: /(?<=#)(?<!^)(\h{6}|\h{3})/ ruby 1.8.7 if that matters
That was only the regex expression. I added the rest of the line.
I'm getting the same error, are you sure this method works on ruby..?
Sure, I ran it before posting. I updated my answer to include a console output.
Looks good, I think my problem is I have a bad ruby binary trying to execute, I cannot even run a plain ruby interpreter, I remember I had a very peculiar time setting this up...thank you.
|

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.