in the regex /(.).*\1|\d/i in ruby, how to also check for hyphens and white-space in ruby

To modify the regex /(.).*\1|\d/i to also check for hyphens and white spaces, you can update it as /(.).*\1|\d|[-\s]/i.

In this updated regex pattern:

  • [-\s] matches any hyphen (-) or white space (\s).

Here's an example usage in Ruby:

main.rb
regex = /(.).*\1|\d|[-\s]/i
string = "Hello - World 123"

if regex.match?(string)
  puts "String matches the pattern"
else
  puts "String does not match the pattern"
end
170 chars
9 lines

This code will output "String matches the pattern" because the given string contains a character repeated, a digit, and a hyphen.

Note that the i at the end of the regex pattern (/i) makes the matching case-insensitive.

Remember to enclose the regex pattern within /.../ and use the match? method to check if the pattern matches the given string in Ruby.

related categories

gistlibby LogSnag