Explain the matching and replacing of strings in Ruby's regular expressions _ruby topics

Source: Internet
Author: User

Regular match

Speaking of Ruby, of course, the regular expression mechanism of the regular expression as a strong matching language has been more and more used in different fields, from string verification, matching, to Web page extraction and so on. Although some people have criticized the matching efficiency with regular expressions, it doesn't matter if the strong matching ability is considered.

Speaking of Ruby regular expressions without having to talk about Ruby's =~ and match two matching methods, let's use examples to illustrate the difference between the two. Let's talk about the use of =~:

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

In Ruby, a regular expression matching statement is represented by//. You can run it, the statement above is a string that matches three digits on both sides of a lowercase letter. We run the code above, and the result will be 10. You must be very strange, why the 10 result, this is the charm of =~, he is to match the results of the first occurrence of the position printed out.

So let's take a look at match here:

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: f414j. The string represents all of the matching rule result sets that the regular statement matches. I don't know if you noticed, we used parentheses in regular expressions, and we wanted to extract three digits. Of course, this is also very simple, we only need to modify the above code slightly:

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 use $ instead of $? Let's look at the results of the $ $.

C:/users/administrator/documents/netbeansprojects/rubyapplication1/lib/regex.rb


It is the output of this object information.

Here's a new scenario, assuming that there's a lot of code-compliant information in a string. The above statement can only match the first found results, and we need to print out all the results? At first I was influenced by Java and thought that the result of match would be a collection, so how to consider it was not the whole. It was later found that there was a scan method. The code looks like this:

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]}"} 

Very simply, the result is:

Theresultis414 
Theresultis223 
Theresultis123 
 
Theresultis414 
Theresultis223 
Theresultis123 

Well, it's very convenient. It's easy to pull out all the matching results.

Grouping of regular expressions

You can group regular expressions and store the grouped values to $1,$2,$3,$4 after the match succeeds ....

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

Regular substitution of strings:

Print "ABCD". Sub (/\w/, "9")  
print "\ n"   
print "ABCD". Gsub (/\w/, "9")  


special Global variables in regular:

    • $1,$2,$3. Group matching text
    • $ ' Match text before text
    • $ ' matches text after text
Print <pre name= "code" class= "Ruby", "\ n", {1}, "\ n", 

if "AB9CD" =~ 

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.