Capture the essence of Ruby Regular Expression usage

Source: Internet
Author: User

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:

 
 
  1. message="afhadhffkdf414j" 
  2. regex=/[a-z](\d{3})[a-z]/  
  3. putsregex=~message  
  4. message="afhadhffkdf414j" 
  5. regex=/[a-z](\d{3})[a-z]/  
  6. 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:

 
 
  1. message="afhadhffkdf414j" 
  2. regex=/[a-z](\d{3})[a-z]/  
  3. putsregex.match(message)  
  4.  
  5. message="afhadhffkdf414j" 
  6. regex=/[a-z](\d{3})[a-z]/  
  7.  
  8. 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:

 
 
  1. message="afhadhffkdf414j" 
  2. regex=/[a-z](\d{3})[a-z]/  
  3. regex.match(message)  
  4. puts$1  
  5.  
  6. message="afhadhffkdf414j" 
  7. regex=/[a-z](\d{3})[a-z]/  
  8. regex.match(message)  
  9. puts$1 

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

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

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:

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

The result is:

 
 
  1. Theresultis414  
  2. Theresultis223  
  3. Theresultis123  
  4.  
  5. Theresultis414  
  6. Theresultis223  
  7. Theresultis123 

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

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.