Introduce regular expressions in ruby in detail

Time:2021-10-27

A regular expression is a special sequence of characters that matches or finds other strings or collections of strings by using patterns with special syntax.
grammar

A regular expression is literally a pattern between slashes or any separator after% R, as follows:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/pattern/
/pattern/im  #You can specify options
%r!/usr/local! #General delimited regular expressions
example
#!/usr/bin/ruby
 
line1 = "Cats are smarter than dogs";
line2 = "Dogs also like meat";
 
if ( line1 =~ /Cats(.*)/ )
 puts "Line1 contains Cats"
end
if ( line2 =~ /Cats(.*)/ )
 puts "Line2 contains Dogs"
end

This will produce the following results:

?
1
Line1 contains Cats

Regular expression modifier

A regular expression may literally contain an optional modifier to control all aspects of matching. The modifier is specified after the second slash character, as shown in the example above. The subscript lists possible modifiers:

Introduce regular expressions in ruby in detail

Just as strings are separated by% Q, ruby allows you to start a regular expression with% R followed by any delimiter. This is useful when describing characters that contain a large number of slashes that you do not want to escape.

?
1
2
3
4
5
6
7
#The following matches a single slash character without escape
 
 
%r|/|      
 
#Flag characters can be matched by the following syntax
%r[</(.*)>]i

Regular expression pattern

Except for the control character (+?. * ^ $() [] {} \), all other characters match themselves. You can escape control characters by placing a backslash before them.

The following table lists the regular expression syntax available in ruby.

Introduce regular expressions in ruby in detail

Introduce regular expressions in ruby in detailIntroduce regular expressions in ruby in detailIntroduce regular expressions in ruby in detailIntroduce regular expressions in ruby in detailIntroduce regular expressions in ruby in detailSearch and replace

Sub and gsub and their substitution variables sub! And gsub! Is an important string method when using regular expressions.

All of these methods use regular expression patterns to perform search and replace operations. Sub and sub! The first appearance of replacement mode, gsub and gsub! Replace all occurrences of the pattern.

Sub and gsub return a new string, keeping the original string unchanged, while sub! And gsub! The string they call is modified.

Here is an example:

?
1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/ruby
 
phone = "2004-959-559 #This is Phone Number"
 
#Delete comments for ruby
phone = phone.sub!(/#.*$/, "")
puts "Phone Num : #{phone}"
 
#Remove characters other than numbers
phone = phone.gsub!(/\D/, ""
puts "Phone Num : #{phone}"

This will produce the following results:

?
1
2
Phone Num : 2004-959-559
Phone Num : 2004959559

Here is another example:

?
1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/ruby
 
text = "rails are rails, really good Ruby on Rails"
 
#Change all "rails" to "rails"
text.gsub!("rails", "Rails")
 
#Change all the words "rails" to uppercase
text.gsub!(/\brails\b/, "Rails")
 
puts "#{text}"

This will produce the following results:

?
1
Rails are Rails, really good Ruby on Rails