Detailed description of matching and replacement operations on strings by regular expressions in Ruby, and ruby Regular Expressions

Source: Internet
Author: User

Detailed description of matching and replacement operations on strings by regular expressions in Ruby, and ruby Regular Expressions

Regular Expression matching

Speaking of Ruby, of course, we need to mention its regular expression mechanism. As a powerful matching language, regular expressions are increasingly used in different fields, from string verification, matching, to web page extraction. Although some people criticize the matching efficiency with regular expressions, it does not matter considering the strong matching ability of regular expressions.

When talking about Ruby regular expressions, we can't help but talk about Ruby's = ~ And match. Let's use an instance to illustrate the differences between the two matching methods. Let's talk about = ~ Usage:

message="afhadhffkdf414j" regex=/[a-z](\d{3})[a-z]/ putsregex=~message message="afhadhffkdf414j" regex=/[a-z](\d{3})[a-z]/ putsregex=~message 

In Ruby, the regular expression matching statement is expressed. You can run the preceding statement to match a string with lowercase letters on both sides of the three numbers. When we run the above Code, The result 10 will appear. You must be surprised, why is the result 10 ~ He is the first appearance position of the matching results to print out.

Let's take a look at the match:

message="afhadhffkdf414j" regex=/[a-z](\d{3})[a-z]/ putsregex.match(message)  message="afhadhffkdf414j" regex=/[a-z](\d{3})[a-z]/  putsregex.match(message) 

Let's take a look at the output result: f414j. This string indicates all the result sets that match the regular expression. I don't know if you have noticed that we have used parentheses in the regular expression. We want to extract three numbers. Of course, this is also very simple. We only need to make some modifications in the above Code:

message="afhadhffkdf414j" regex=/[a-z](\d{3})[a-z]/ regex.match(message) puts$1  message="afhadhffkdf414j" regex=/[a-z](\d{3})[a-z]/ regex.match(message) puts$1 

The result is 414. Why $1 instead of $0? Let's look at the result of $0.

C:/Users/Administrator/Documents/NetBeansProjects/RubyApplication1/lib/regex.rb

 
It is the output object information.

Next, a new situation occurs. Suppose a string contains a lot of information that complies with the rules. The preceding statement can only match the first result. What should we do if we print all the results? At the beginning, I was influenced by java and thought that the match result would be a set, so I didn't think about it. Later I found a scan method. The Code is as follows:

message="afhadhffkdf414j9tr3j43i3433094jwoert223jwew123dfdf" regex=/[a-z](\d{3})[a-z]/ message.scan(regex).each{|m|puts"Theresultis#{m[0]}"}  message="afhadhffkdf414j9tr3j43i3433094jwoert223jwew123dfdf" regex=/[a-z](\d{3})[a-z]/ message.scan(regex).each{|m|puts"Theresultis#{m[0]}"} 

The result is:

Theresultis414 Theresultis223 Theresultis123  Theresultis414 Theresultis223 Theresultis123 

It's very convenient. It's easy to extract all the matching results.

Grouping of Regular Expressions

You can group a regular expression and store the value of the Group to $1, $2, $3, $4 .......

print $1,"\n",$2 if "a1b2c3d4e5" =~ /(\w{2})(\w*)/  

String Regular Expression replacement:

print "abcd".sub(/\w/,"9")  print "\n"   print "abcd".gsub(/\w/,"9")  


Special global variables in Regular Expressions:

  • $1, $2, $3... group match text
  • $ 'Match the text before the text
  • $ 'Match text
print <pre name="code" class="ruby">,"\n",{1},"\n", if "ab9cd" =~ /\d/ 

Articles you may be interested in:
  • Analysis on the use of regular expressions in Ruby
  • Basic instructions on using regular expressions in Ruby
  • Example: Regular Expressions in Ruby
  • Introduce regular expressions in Ruby in detail
  • Use the Ruby re module to create complex Regular Expressions
  • Ruby Regular Expression tutorial

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.