-
Regular expression:
Regular expressions allow users to construct a matching pattern by using a series of special characters, and then compare the matching pattern with target objects such as data files, program input, and form input on WEB pages. Depending on whether the comparison object contains the matching pattern, execute the corresponding program. Regularity has won the love of programmers for its simplicity, practicality and speed. 
Some thoughts on writing regex:
1. First, determine the rules. What kind of string do you need to match, what parts does it consist of, and what characteristics does it have.
2. Propose the smallest unit. Sometimes, some matching rules are repeated. We can try to find common points and plan them into a rule.
3. Write the regular rules one by one according to their rules and smallest units.
4. Unify the combination of these unit regularities to form the complete regularity you want.
Here we need to understand some basic tags and usage. Any rules are composed of basic tags. The regular markup is also very simple, so you might as well understand it.
Commonly used tags in regular expressions
1 Commonly used markers in regular expressions 2 ^ | Beginning of a line or string 3 $ | End of a line or string 4 . | Characters other than newlines 5 w | Word characters (numbers, letters, or underscores) 6 W | Non-word characters 7 d | Numbers (equivalent to [0-9]) 8 D | Non-whitespace characters 9 10 A | Beginning of string 11 Z | End of string or last newline character 12 z | Characters End of string 13 14 s | Blank character 15 S | Non-blank character 16 17 b | Word boundary (outside []) 18 B | Non-word boundary 19 b | Backspace (inside []) 20 [] | Any character in the set, with a hyphen in the middle, represents a range, such as: [0-9], [az]21? | 0 or 1 previous expression 22 | | or 23 () | Subexpression Grouping 24 25 * | zero or more previous expressions 26 *? | zero or more previous expressions (non-greedy) 27 + | 1 or more previous expressions 28 +? | 1 or more Previous expressions (non-greedy) 29 {m,n} | mn previous expressions 30 {m,n}? | mn previous expressions (non-greedy) 31 32 33 Also {?=} { ?!} (?>) (?#) (?imx-imx) etc.
The most basic example:
1 # Match the entire string 2 puts /ruby/ =~ "ruby" # 0 3 puts /ruby/ =~ "Ruby" # nil 4 5 # Match the beginning and end of a line or string 6 str = "abcdef" 7 puts str 8 puts /^abc/ =~ str # 0 9 puts /def$/ =~ str # 310 11 str = "abcndefn" 12 puts /^abc/ =~ str # 013 puts /def$/ =~ str # 414 puts /abc/ =~ str # 015 puts /def/ =~ str # 416 17 #Match the beginning and end of the string itself 18 str = "abcndefn" 19 puts /Aabc/ =~ str # 020 puts /defZ/ =~ str # 421 puts /defz/ =~ str # nil22 23 #Match word characters 24 puts "Match word characters" 25 pattern = /w/26 puts pattern =~ "abc " # 027 puts pattern =~ "." # nil28 29 # Match integer 30 # d Number 31 # D Non-number 32 puts "Match number" 33 puts /d/ =~ "122" #034 puts /D / =~ "122" #nil35 puts /D/ =~ "abc" #036 37 38 #Border 39 str = "This is your friend!"40 puts str41 puts str.gsub(/b/,"|" ) # |This| |is| |your| |friend|!42 puts str.gsub(/B/,"-") # This is your friend!-43
Qualifier instance
1 puts "qualifier" 2 puts "qualifier: *" 3 puts /(abc)*/ =~ "a" # 0 4 puts /(abc)*/ =~ "abc" # 0 5 6 7 puts "qualifier Qualifier: +" 8 puts /(abc)+/ =~ "a" # nil 9 puts /(abc)+/ =~ "abc" # 010 11 puts "Qualifier: ?" 12 puts /(abc)?/ =~ "c" # 013 puts /(abc)?/ =~ "abc" # 014 15 16 puts "Qualifier: {n}"17 puts /(abc){2}/ =~ "abc" # nil18 puts /(abc){2}/ =~ "abcabc" # 019 20 puts "Qualifier: {n,}"21 puts /(abc){2,}/ =~ "abc" # nil22 puts /(abc){ 2,}/ =~ "abcabc" # 023 24 puts "Qualifier: {n,m}"25 puts /(abc){2,3}/ =~ "abc" # nil26 puts /(abc){2, 3}/ =~ "abcabcabc" # 027 28 puts "Qualifier: *?" 29 puts /(abc)*?/ =~ "a" # 030 puts /(abc)*?/ =~ "abcabc" # 031 32 puts "Qualifier: +?" 33 puts /(abc)+?/ =~ "a" # nil34 puts /(abc)+?/ =~ "abcabc" # 035 36 puts "Qualifier: ??"37 puts /(abc)??/ =~ "a" # 038 puts /(abc)??/ =~ "abcabc" # 039 40 41 #match, {1} {3}42 # {3} can match, then {1} will definitely match, but the reverse is not true 43 # Matching range: {1} > {3}
Common examples:
1 puts "example" 2 #Match phone numbers: 3 #Generally, Chinese phone numbers are 7-8 digits, area codes are 3-4 digits integers, the first digit of the area code is 0, and the separator '-' is used in the middle 4 #Pay attention to the first and last restrictions Symbol 5 pattern = /^0d{2,3}-d{7,8}$/ 6 puts pattern =~ "010-82809999" # 0 7 puts pattern =~ "00010-82809999" # nil 8 9 #Match mobile phone number 10 # Starting with 1, the second digit is 3, 5, 8, 11 digits 11 pattern = /^1[3,5,8]d{9}$/12 puts pattern =~ "15810990001" #013 14 #ID card number 15 pattern = /d{18}|d{15}/16 17 #Match IP18 #Four groups of integers not greater than 255, separated by '.' 19 puts "IP" 20 num = /^d|[01]?d{1,2}|2[0-4]d|25[0-5]/21 pattern = /^(#{num}.){3} #{num}$/22 #A whole: pattern = /^(^d|[01]?d{1,2}|2[0-4]d|25[0-5].) {3}^d|[01]?d{1,2}|2[0-4]d|25[0-5]$/23 puts pattern =~ "127.0.0.1" # 024 puts pattern =~ "254.255.255.255"25 26 #Match email address 27 pattern = /^w+@w+.w+$/ #This does not have '-'28 pattern = /^[w-]+@[w- ]+.[w-]+$/29 puts pattern =~ " [email protected] " #030 31 # Match url32 # http://www.google.cn33 pattern = /(http|https|ftp): (//|\\)((w)+[.]){1,}(net|com|cn|org|cc|tv|[0-9]{1,3})( ((/[~]*|\[~]*)34 (w)+)|[.](w)+)*(((([?](w)+){ 1}[=]*))*((w)+){1}([&](w)+[=](w)+)*)*/35 36 puts pattern =~ " http://www.google.cn?uid=123 " #0
other
The meaning of greed
1 puts "greedy" 2 #Greedy means matching the longest possible string 3 # '.' Meaning: characters other than newlines 4 str = "where the sea meets the moon-blanch'd land" 5 puts str 6 match = /.*the/.match(str) 7 puts match[0] # where the sea meets the, * is greedy. If you don't want to be greedy, add + '?' 8 #*?non-greedy 9 match = /.*?the/.match(str)10 puts match[0] # where the11 12 str.insert(0,"Hello n")13 match = /.*?the/.match(str)14 puts match[0] #where the
Positive and negative preview
1 #Positive and negative preview 2 puts "Positive and negative preview" 3 s1 = "New World Dictionary" 4 s2 = "New World Symphony" 5 s3 = "New World Order" 6 7 reg = /New World (?=Dictionary| Symphony)/ 8 m1 = reg.match(s1) 9 puts m1.to_a[0] # "New World"10 m2 = reg.match(s2)11 puts m2.to_a[0] # "New World"12 puts reg .match(s3) # nil13 14 puts "negative preview"15 16 reg = /New World (?!Symphony)/ # There is no "Symphony" after "New World" 17 puts reg.match(s1).to_a[0] # "New World"18 puts reg.match(s2).to_a[0] # nil19 puts reg.match(s3).to_a[0] # "New World"
-