Ruby - Regular Expressions Pattern Matching

Introduction

To check whether a certain string matches against the pattern, you can use string value match.

To check if a string contains any vowels:

Demo

puts "String has vowels" if "This is a test" =~ /[aeiou]/

Result

Here, =~ is a matching operator.

If the string has a match with the regular expression following the operator, then the expression returns the position of the first match.

To do the opposite:

Demo

puts "String contains no digits" unless "This is a test" =~ /[0-9]/

Result