A preliminary explanation of regular expressions in Ruby

Source: Internet
Author: User
Tags regular expression ruby on rails

This article mainly introduces the regular expression in Ruby, is the important knowledge of Ruby learning, need friends can refer to the following

Regular expressions are a special sequence of characters that can help match or find other strings or string sets, using patterns that maintain a specialized syntax.

The regular expression literal is a pattern between a slash or any delimiter%r as follows:

Grammar:

Copy code code as follows:

/pattern/

/PATTERN/IM # option can be specified

%r!/usr/local! # General Delimited Regular expression

For example:

?

1 2 3 4 5 6 7 8 9 10 11 #!/usr/bin/ruby line1 = "Cats are smarter than";   Line2 = "Dogs also like meat"; if (line1 =~/cats (. *)/) puts Line1 starts with Cats-End if (line2 =~/cats (. *)/) puts ' Line2 starts with Dogs ' end

This will produce the following results:

?

1 Line1 starts with Cats

Regular expression modifiers:

The text of a regular expression can include an optional modifier to control all aspects of the match. After you modify the specified second slash character, as shown earlier, you can represent one of these characters:

%q separates string literals, Ruby allows regular expressions to be%r and then delimited by the selected delimiter. This is useful when the described pattern contains a forward slash character that does not want to be escaped:

?

1 2 3 4 5 # following matches a single slash character, no escape required%r|/| # Flag characters are allowed with this syntax, too%r[</(. *) >]i

Regular expression pattern:

Except for the control character, (+?. * ^ $ () [] {} |), all characters match. You can escape the control character preceded by a backslash.

Search and Replace:

The string method is most important, using regular expression sub and gsub, they are in situ variants sub! and gsub!

All of these methods use a regular expression pattern during a search and replace operation. Sub & sub! Replaces all occurrences of the pattern Gsub & gsub! that appear for the first time.

sub! and gsub! Returns a new string, unmodified original sub and gsub they are called modified strings.

The following example:

?

1 2 3 4 5 6 7 8 9 10 11 #!/usr/bin/ruby phone = "2004-959-559 #This is phone number" # Delete ruby-style Comments Phone = phone.sub! (/#.*$/, "") puts "phone Num: #{phone}" # Remove anything other than digits 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 ' rails ' to ' rails ' throughout text.gsub! ( "Rails", "rails") # capitalize the word "rails" throughout text.gsub! (/brailsb/, "Rails") puts "#{text}"

This will produce the following results:

?

1 Rails are rails, really good Ruby on rails
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.