So, we will be taking the String, Beautiful Nature,
-
'^' Symbol
The ^ symbol is used to check if a String starts with a certain character or characters.
Say for example, let us take the above String,
And let us check if the above String, starts with the letter B or Bea.
Let us see in the below example,
Example :
str = "Beautiful Nature"
x = str.match("^B")
if x
puts "The String ::Beautiful Nature:: starts with B"
else
puts "The String ::Beautiful Nature:: does not start with B"
end
y = str.match("^Bea")
if y
puts "The String ::Beautiful Nature:: starts with Bea"
else
puts "The String ::Beautiful Nature:: does not start with Bea"
end
Output :
The String ::Beautiful Nature:: starts with B
The String ::Beautiful Nature:: starts with Bea
So, in the above example, we have taken the String Beautiful Nature and initialised it to a variable str.
So, at first we have checked, if the above String starts with the letter B.
And this is where we have used the Method match().
So, the match() Method accepts two arguments, the pattern and the actual String.
Now, let us come to the ^ Symbol, which is used in the pattern,
That actually says, "Check if the String starts with the letter B".
And in this case the String starts with B. So, the control enters the blocks of if statement,
if x
puts "The String ::Beautiful Nature:: starts with B"
And prints the value,
The String ::Beautiful Nature:: starts with B
Similarly, we have used the ^ Symbol in the next line,
To check, if the String begins with Bea.
And in this case, the String begins with Bea. So, the control enters the blocks of if,
if y
puts "The String ::Beautiful Nature:: starts with Bea"
And the below output is printed on the screen,
The String ::Beautiful Nature:: starts with Bea
Example |
Description |
str = "Beautiful Nature"
x = str.match("^e")
|
There is no match as the String doesn't
begin with 'e'. But it begins with 'B'
|
str = "Beautiful Nature"
x = str.match("^Bae")
|
There is no match as the String doesn't
begin with 'Bae'.
|
-
'$' Symbol
The $ symbol is used to check if a String ends with a certain character or characters.
Say for example, let us take the above String,
And let us check if the above String, ends with the letter e or ture.
Let us see in the below example,
Example :
str = "Beautiful Nature"
x = str.match("e$")
if x
puts "The String ::Beautiful Nature:: ends with e"
else
puts "The String ::Beautiful Nature:: does not end with e"
end
y = str.match("ture$")
if y
puts "The String ::Beautiful Nature:: ends with ture"
else
puts "The String ::Beautiful Nature:: does not end with ture"
end
Output :
The String ::Beautiful Nature:: ends with e
The String ::Beautiful Nature:: ends with ture
So, in the above example, we have taken the String Beautiful Nature and initialised it to a variable str.
So, at first we have checked, if the above String ends with the letter e.
That actually says, "Check if the String ends with the letter e".
And in this case the String ends with e. So, the control enters the blocks of if statement,
if x
puts "The String ::Beautiful Nature:: ends with e"
And prints the value,
The String ::Beautiful Nature:: ends with e
Similarly, we have used the ^ Symbol in the next line,
To check, if the String ends with ture.
And in this case, the String ends with ture. So, the control enters the blocks of if,
if y
puts "The String ::Beautiful Nature:: ends with ture"
And the below output is printed on the screen,
The String ::Beautiful Nature:: ends with ture
Example |
Description |
str = "Beautiful Nature"
x = str.match("B$")
|
There is no match as the String doesn't
end with 'B'. But it ends with 'e'
|
str = "Beautiful Nature"
x = str.match("tur$")
|
There is no match as the String doesn't
ends with 'tur'.
|
-
'.' Symbol
The . symbol is used to check if a String matches any character except a new line.
Say for example, let us take the above String,
And let us say, you know there is a String but you forgot the spelling of Beautiful. You can remember it is, Beaut and it ends with an l.
In such case . operator comes to rescue. Just substitute the unknown characters with .. And you can search for the actual String.
The above . dots will be replaced by the actual value.
Let us see in the below example,
Example :
str = "Beautiful Nature"
x = str.match("Beaut...l")
puts "The string is : #{x}"
Output :
The string is : Beautiful
So, in the above example, we have taken the String Beautiful Nature and initialised it to a variable str.
And checked, if at all there is a word, that starts with Beaut and ends with an l.
x = str.match("Beaut...l")
And Ruby checks the actual String,
And finds that there is a word, Beautiful that starts with Beaut and ends with an l. And the three dots '.' could be replaced by i, f and u.
So, the word Beautiful is fetched and put to the variable x.
x = str.match("Beaut...l")
Note : You have to put the exact number of dots . as the number of characters, else the pattern won't return the exact String.
And the print statement,
Prints the List,
The string is : Beautiful
Example |
Description |
str = "Hello"
x = x = str.match(".....")
|
There is a match as the String has a 5
letter word 'Hello' that matches with
the 5 '.'s
|
str = "Beautiful"
x = x = str.match(".....") |
There is no match as the String
'Beautiful' is a 9 letter word that
doesn't match 5 '.'s
|
-
'+' Symbol
The + is used to check one or more occurrences of a pattern.
Let us take a different String to understand the + Symbol,
And let us say, you know there is a String but you forgot the spelling of Cool. And you are confused, if there is one o or two os in the string Cool.
In such case + Symbol comes to rescue. Just place the + symbol after o.
And no matter, how many 'o's are there. The exact String would be returned.
Let us see in the below example,
Example :
str = "Cool Guy"
x = str.match("Co+l")
puts "The string is : #{x}"
Output :
The string is : Cool
So, in the above example, we have checked, if at all there is a word, that starts with Co and ends with an l. No matter how many os are there.
And Ruby checks the actual String,
And finds that there is a word, Cool that starts with Co and ends with an l. And there is one o that can be substituted.
So, the word Cool is fetched and put to the variable x.
And the print statement,
puts "The string is : #{x}"
Prints the value of x as List,
Even if there were multiple 'o's in the String. The match would be found.
Example :
str = "Cooooool Guy"
x = str.match("Co+l")
puts "The string is : #{x}"
Output :
The string is : Cooooool
Example |
Description |
str = "Heo" |
There is no match as the String 'Heo' |
x = str.match("Hel+o") |
doesn't have an 'l' in it. |
-
'*' Symbol
The * is same as + Symbol with a mild difference. It is used to check zero or more occurrences of a pattern.
Let us take the below String to understand the * Symbol,
And let us say, you know there is a String but you forgot the spelling of Cool. And you are confused, if there is one o or two os in the string Cool.
In such case * Symbol comes to rescue. Just place the * symbol after o.
And no matter, how many os are present. The exact String would be returned.
Let us see in the below example,
Example :
str = "Cool Guy"
x = str.match("Co*l")
puts "The string is : #{x}"
Output :
The string is : Cool
So, in the above code, we have checked, if at all there is a word, that starts with Co and ends with an l. No matter how many os are there.
And Ruby checks the actual String,
And finds that there is a word, Cool that starts with Co and ends with an l. And there is one o that can be substituted.
So, the word Cool is fetched and put to the variable x.
And the print statement,
puts "The string is : #{x}"
Prints the value of x as List,
So, how is * Symbol different from + Symbol?
Let us consider the String where there is no os at all in the String.
Example :
str = "Cl Guy"
x = str.match("Co*l")
puts "The string is : #{x}"
Output :
The string is : Cl
That is because the * Symbol searches for zero or more occurrences.
So, the String,
Doesn't have any os in it. Still the pattern,
Is able to search the String Cl.
Example |
Description |
str = "Heleo"
x = str.match("Hel*o")
|
There is no match as the String 'Heleo'
has an 'e' between 'l' and 'o'. |
str = "Helo"
x = str.match("Hel*o")
|
There is a match as the String 'Helo'
has one 'l' in it.
|
-
'?' Symbol
The ? is same as + and * Symbol with a mild difference. It is used to check zero or exactly one occurrences in a pattern.
Let us take the below String to understand the ? Symbol,
So, at first, let us understand, how is ? Symbol different from + and * Symbol.
And as we have done for the * Symbol, you know there is a String but you forgot the spelling of Cool.
And you are confused, if there is one o or two os in the string Cool.
Let's see, how ? symbol responds in such case.
Let us see in the below example,
Example :
str = "Cool Guy"
x = str.match("Co?l")
puts "The string is : #{x}"
Output :
The string is :
And we got an empty String.
This is because the below line,
Searches for the pattern Co?l.
And ? Symbol searches for zero or one occurrence of o(i.e. Co?l). But in the String Cool Guy, The substring Cool has two os. So it returns nothing.
So, when should ? Symbol find a match.
Let us consider the String where there is no os or 1 o in the String.
Or
Example :
str = "Cl Guy"
x = str.match("Co?l")
puts "The string is : #{x}"
Output :
The string is : Cl
Example |
Description |
str = "Heleo"
x = str.match("Hel?o")
|
There is no match as the String 'Heleo'
has an 'e' between 'l' and 'o'.
|
str = "Hello"
x = str.match("Hel?o")
|
There is no match as the String 'Hello'
has two 'l's in it.
|
-
'|' Symbol
The | is like OR.
Say for example, let us take the String,
And let us say, you want to check, how many times o and u is there in the string?
In such case | Symbol is the option. Just give the option of u|o(i.e. u or o).
And no matter, where u or o is. Ruby finds them.
Let us see in the below example,
Example :
str = "Cool Guy"
x = str.scan(/u|o/)
puts "The result is : #{x}"
Output :
The result is : ["o", "o", "u"]
So, in the above example, we have checked, how many times o and u is there in the string?
And this time we have used the scan() method because we will be getting multiple results as an Array.
The syntax is a little different. Instead of double quotes "". We would be using slash //.
And Ruby checks the actual String,
And finds that there is a word, Cool has two os and the word, Guy has one u.
Now, if you see the output,
The result is : ["o", "o", "u"]
The Array x has three items in it. i.e. o, o and u.
We can also search for a String using | Symbol.
Say for example, you forgot if the String is Cool Guy or Fool Guy.
In that case we can use the | operator.
Example :
str = "Cool Guy"
x = str.scan(/Cool|Fool/)
puts "The string is : #{x}"
Output :
The string is : ['Cool']
So, in the above code we wanted to check, if the String Cool Guy.
Contains the word, Cool or Fool.
x = str.scan(/Cool|Fool/)
And since, there is just one match for Cool. The variable x would have one item in the Array i.e. Cool.
Example |
Description |
str = "Heleo"
x = str.scan(/P|u/)
|
There is no match as the String 'Heleo'
has no 'P' or 'u' in it.
|
-
'()' Symbol
The () is used to group Substrings.
Say for example, let us take a different String,
Now, let us say, you want to check, how many times the letters w or o is followed by the letter l.
In such case () Symbol is the option.
Let us see in the below example,
Example :
str = "Cool Owl"
x = str.scan(/(w|o)l/)
puts "The elements in the Array are : #{x}"
Output :
The elements in the Array are : [["o"], ["w"]]
So, in the above example, we have checked, how many times the letters w or o is followed by the letter l.
And we have placed the letters w and o inside the () Symbol. Also if you note, we have used the | (i.e. or) Symbol.
Now, Ruby checks the actual String, and tries to find a match where the the letters w or o is followed by the letter l.
And finds that there is are two words, Cool and Owl.
Where o is followed by l in Cool. And w is followed by l in Owl.
Now, if you see the output,
The elements in the Array are : [["o"], ["w"]]
The Array x has two items in it. i.e. o and w. First element o is the match for Cool. And the second match w is the match for Owl.
Example |
Description |
str = "Hello Mellow Howl"
x = str.scan(/(l|H)o/)
|
There is 3 matches as the String
'Hello Mellow Howl' has 'lo', 'lo' &
'Ho'.
|
str = "Hello"
x = str.scan(/(H|e)o/)
|
There is no match as the String 'Hello'
has no match for 'Ho' or 'eo'.
|
-
'{}' Symbol
The {} is used to match the exact number of occurrences of a letter in a String.
Say for example, let us take the String,
Now, let us say, you want to check, if the letter C is followed by two os.
In such case {} Symbol is the option.
Let us see in the below example,
Example :
str = "Cool Guy"
x = str.scan(/Co{2}/)
puts "The element in the Array is : #{x}"
Output :
The element in the Array is : ["Coo"]
So, in the above example, we have checked, if the letter C is followed by two os.
And we have placed the number 2 inside the {} Symbol.
Now, Ruby checks the actual String, and tries to find a match where the letter C is followed by two os.
And finds that there is are one match, Coo.
Now, if you see the output,
The element in the Array is : ["Coo"]
There is just one match i.e. Coo.
Next, let us take another String.
Now, let us say, you want to check, if the String has one o or two os.
In such case, we will use the {} Symbol with two numbers.
Let us see in the below example,
Example :
str = "Cool owl"
x = str.scan(/o{1,2}/)
puts "The elements in the Array are : #{x}"
Output :
The elements in the Array are : ["oo", "o"]
So, in the above example, we have checked, if the String,
Has one o or two os.
And we have placed the number 1,2 inside the {} Symbol(i.e. o{1,2}).
Now, Ruby checks the actual String, and tries to find a match where the letter o is present once or twice.
Now, if you see the output,
The elements in the Array are : ["oo", "o"]
The Array x has two items in it. i.e. oo and o. First element oo is the match for Cool. And the second match o is the match for owl.
-
0. '[]' Symbol
The [] is used to match a set of characters in a String.
Say for example, let us take the String,
Now, let us say, you want to check, if the letters o, G, z and y are present in the String or not.
In such case [] Symbol is the option.
Let us see in the below example,
Example :
str = "Cool Guy"
x = str.scan(/[oGzy]/)
puts "The elements in the Array are : #{x}"
Output :
The elements in the Array are : ["o", "o", "G", "y"]
So, in the above example, we have checked, if the letters o, G, z and y are present in the String or not.
And we have placed all the letters o, G, z and y inside the [] Symbol.
Now, Ruby checks the actual String, and tries to find a match for all the letters o, G, z and y inside the []
And finds that there is are four matches.
Now, if you see the output,
The elements in the Array are : ["o", "o", "G", "y"]
You can find that there are four matches. i.e. Two match for o, and one match for G and y. But no match for z as z is not present in the String.
Use of '-' with '[]'
We can use - with the [] symbol. Which is short form of to.
The below pattern:
Says, search for all the characters from a to f(i.e. a, b, c, d, e and f).
Similarly, the pattern :
Says, to search for all the numbers from 0 to 100.
Also, the pattern :
Says, to search all the numbers from 00 to 99
Use of '^' with '[]'
We can also use ^ with the [] symbol. Which is short form of not.
The below pattern:
Says, search for all the characters except the letters o, G, z and y.
Example :
str = "Cool Guy"
x = str.scan(/[^oGzy]/)
puts "The elements in the Array are : #{x}"
Output :
The elements in the Array are : ["C", "l", " ", "u"]
So, if you see the above output, only the letters o, G, z and y are excluded from the String Cool Guy.
-
1. '\' Symbol
\ Symbol is said to be an escape character.
Say for example, you have a sentence,
And you are supposed to search if there is a question mark(i.e. ?) after the substring you(i.e. you?)
But as we have seen ? is a Meta-Character.
Now, if you don't want ? to behave as a Meta-Character. You can use \ Symbol before it.
Let us see in the below example :
At first let us see, what happens if you don't put the escape symbol \
Example :
str = "How are you?"
x = str.scan(/you?$/)
puts "The elements in the Array are : #{x}"
Output :
The elements in the Array are : []
So, all we have done is, checked if the String How are you?.
Ends with you?.
But if you see the output, we got an empty Array.
The elements in the Array are : []
That is because ? is a Meta-Character. So, it gets confused and returns nothing.
Let's correct it in the below example.
Example :
str = "How are you?"
x = str.scan(/you\?$/)
puts "The elements in the Array are : #{x}"
Output :
The elements in the Array are : ["you?"]
And as we can see, we have just placed the escape symbol \ before ?.
So that ? is not treated as a Meta-Character.
And we got the desired output.
The elements in the Array are : ["you?"]