I was started learning the regular expressions in ruby. In that I had one problem. The problem is the below regular expression does not work as expected.
/^[\s]*$/ -- This will match only if the input contains white spaces or the input contains empty.
str = "
if str =~ /^[\s]*$/
puts "Condition is true"
puts "Condition is false"
The reason is that in Ruby regex,
$ match the start/end of a line. Change to
\z and you will get a
See this Ruby demo at Ideone. The
/\A\s*\z/ will only match strings that are either empty or have whitespace symbols only.
\s, it is a synonym for
[ \t\r\n\f], not just
[ \t\n]. See this Ruby Character Class reference:
/\s/- A whitespace character: