Overview
Dot ‘.’ character is one of the most commonly used metacharacters in the regular expression. It is used to match any character. By default, it doesn’t match a new line.
Now let’s see a simple program for Dot ‘.’ character
Program
match = "a".match(/./)
puts "For a: " + match.to_s
match = "b".match(/./)
puts "For b: " + match.to_s
match = "ab".match(/./)
puts "For ab: " + match.to_s
match = "".match(/./)
puts "For Empty String: " + match.to_s
Output
For a: a
For b: b
For ab: a
For Empty String:
In the above program, we have a simple regex containing only one dot character.
/./
It matches below characters and string.
a
b
ab
It matches ab because by default the regex doesn’t do the match the full string unless we use the anchor characters (Caret and Dollar character). That is why it matches the first character ‘a’ in ‘ab’ and reports a match.It doesn’t match an empty string.
Let’s see another example where we have two dots in the regex.
match = "ab".match(/../)
puts "For ab: " + match.to_s
match = "ba".match(/../)
puts "For ba: " + match.to_s
match = "abc".match(/../)
puts "For abc: " + match.to_s
match = "a".match(/../)
puts "For a: " + match.to_s
Output
For ab: ab
For ba: ba
For abc: ab
For a:
In the above program, we have a simple regex containing two dots.
/../
It will match any given string which has at least two characters as a substring.
That is why it gives a match for
ab
ba
abc
and doesn’t give a match for (It gives an empty string match)
a