Regular Expressions

Regular expressions are basically used to work with character/text patterns in ruby.

Regular Expressions  in Ruby

sub() method isn't a regular expression. But it explains when to use the regular expressions. It replaces part of string with other string specified by the user. It contains two parameters, the first parameter is the substring which is to be replaced and second parameter is the substring that you should replace with.

For example, to replace the substring bad in badjohnny with good we can use:

puts "badjohnny".sub('bad','good')

It replaces bad with good. But it doesn't replace all the instances of the substring.

Regular Expressions  in Ruby

You could see that it replaces only the first instance of the substring bad. To replace all the instances of substring gsub() method is used.

Regular Expressions  in Ruby

Things like these can be done in Regular expressions.

Regular Expressions:

Regular Expressions in Ruby

Variable a has the value "Hey, what do you want?". If you want to replace the "Hey" with "Excuse me" then you can use regular expressions. As already said, sub() method have two parameters. We have to replace the first three characters with our desired string.

The first parameter contains /^.../

Regular expression starts and ends with forward slash /. ^ symbol is used to match the substring from the beginning of the string. It didn't replace the characters in the middle with, it replaced the characters from the beginning. Dots . represents any characters, it may be numbers, alphabets, symbols anything

Now, what it does is, it matches 3 characters from the beginning in our case it is "Hey" and replaces it with the string specified by the user.

Regular Expressions in Ruby

Scan method:

Variable a contains some value. Using scan() method, we are going to scan the variable a and using regular expression, we read the characters and display it in the way we want.

The regular expression (/.../) reads three characters and stores it temporarily in variable x, using that value we can do our desired operation. As said before, dots represent any character so it reads any three characters (including spaces). We've just printed the value of x.

Scan method in Ruby

Here inside regular expressions we have used '\S'. \S is a non-white space character. It can be a number or any letter. We have given three \S\S\S, it iterates through the variable and finds three non-white space characters and stores in the variable x.

First it reads "Rub" stores in x and prints it. Then it reads "y" that is y and a space it ignores it because it contains white space characters. Likewise, "a" and "l" in typical is ignored.