Ruby | Regular Expressions (original) (raw)

Last Updated : 17 Sep, 2019

A regular expression is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings. Ruby regular expressions i.e. Ruby regex for short, helps us to find particular patterns inside a string. Two uses of ruby regex are Validation and Parsing. Ruby regex can be used to validate an email address and an IP address too. Ruby regex expressions are declared between two forward slashes.Syntax:

finding the word 'hi'

"Hi there, i am using gfg" =~ /hi/

This will return the index of first occurrence of the word 'hi' if present, or else will return ' nil '.

Checking if a string has a regex or not

We can also check if a string has a regex or not by using the match method. Below is the example to understand.Example :

Ruby `

Ruby program of regular expression

Checking if the word is present in the string

if "hi there".match(/hi/) puts "match" end

`

Output:

match

Checking if a string has some set of characters or not

We can use a character class which lets us define a range of characters for the match. For example, if we want to search for vowel, we can use [aeiou] for match.Example :

Ruby `

Ruby program of regular expression

declaring a function which checks for vowel in a string

def contains_vowel(str) str =~ /[aeiou]/ end

Driver code

Geeks has vowel at index 1, so function returns 1

puts( contains_vowel("Geeks") )

bcd has no vowel, so return nil and nothing is printed

puts( contains_vowel("bcd") )

`

Output:

1

Different regular expressions

There are different short expressions for specifying character ranges :

Example :

Ruby `

Ruby program of regular expression

a="2m3" b="2.5"

. literal matches for all character

if(a.match(/\d.\d/)) puts("match found") else puts("not found") end

after escaping it, it matches with only '.' literal

if(a.match(/\d.\d/)) puts("match found") else puts("not found") end

if(b.match(/\d.\d/)) puts("match found") else puts("not found") end

`

Output:

match found not found match found

Modifiers for regex

For matching multiple characters we can use modifiers: