Using Ruby 1.9.2, I have the following Ruby code in IRB:
> r1 = /^(?=.*[\d])(?=.*[\W]).{8,20}$/i > r2 = /^(?=.*\d)(?=.*\W).{8,20}$/i > a = ["password", "1password", "password1", "pass1word", "password 1"] > a.each {|p| puts "r1: #{r1.match(p) ? "+" : "-"} \"#{p}\"".ljust(25) + "r2: #{r2.match(p) ? "+" : "-"} \"#{p}\""}
The result is the following:
r1: - "password" r2: - "password" r1: + "1password" r2: - "1password" r1: + "password1" r2: - "password1" r1: + "pass1word" r2: - "pass1word" r1: + "password 1" r2: + "password 1"
1.) Why are the results different?
2.) Why r1 correspond to lines 2, 3, and 4? Wouldn't the search (?=.*[\W]) fail because there are no characters without words in these examples?
source share