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:




#!/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:




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:




# 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:




#!/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:




Phone num:2004-959-559 phone num:2004959559


Here is another example:




#!/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:



Rails are rails, really good Ruby on rails
Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.