Regular expression, 8 principles (turn 9CBS)

xiaoxiao2021-03-06  50

1. Three forms of regular expressions first we should know that in the Perl program, there are three forms of presence, they are: Match: m / / (still abbreviated as / /, slightly m) Replacement: S / / / Transformation: TR / / / These three forms are generally with = ~ or! ~ match (where "= ~" indicates match, Read as DOES, "!!!!" In the whole statement does not match, read as Doesn't in the whole statement), and the scalar variable to be processed on the left side. If there is no such variable and = ~! ~ Operator, the contents of the $ _ variable will be derived by default. Also: Foreach (@Array) {S / A / B /;} # This time you will remove an element from the @Array array in each cycle in the $ _ variable, and replace the $ _. While () {print if (m / error /);} # This sentence is slightly complex, and he will print all the lines containing the Error string in the file file. Replacement operation S / / / can also add E or G parameters at the end, and their meaning is: S / / / g represents all in line with all in line with all the inquiry strings Pattern> The pattern is all replaced with the string, not only the first mode. S / / / e indicates that the portion is used as an operator, and this parameter is not used. 2 General mode in the regular expression is some of the common modes in the regular expression.

/ pattern / result. Match all character x? Match 0 times or once X * matches 0 times or multiple times, but match the minimum number of X match 1 or more X characters String, but match the least least. * Match 0 or once any character. Match 1 or more characters {m} matches just M, specified strings {m, n} matches M, The specified string of the above nies is {m,} matches M or more specified strings [] matching characters in [] matching characters in [] matching characters in [0-9] match all numeric characters [ AZ] Match all lowercase letters characters [^ 0-9] Match all non-digital characters [^ AZ] match all non-written alphanumeric characters ^ Match the character of the character $ Match the character / d Matched a number characters, and [ 0-9] Syntax, same / d match multiple numeric strings, and [0-9] syntax, the same / D non-digital, other same / d / d non-digital, other same / d / w English letters or characters String, and [A-ZA-Z0-9] syntax, same / w and [A-ZA-Z0-9] syntax, same / W non-English letters or numbers, and [^ A-ZA-Z0-9 ] Syntax is the same / w and [^ a-za-z0-9] syntax, same / s space, and [/ n / t / r / f] syntax like / s and [/ n / t / r / f] The same / s non-space, and the [^ / N / T / R / F] syntax like / s and [^ / n / t / r / f] syntax same / b match the character string of English letters, numbers as boundary / B matches the string A | B | C matching the A-character string ABC matches the ABC, which matches the ABC, which matches the ABC, or the C character, or a string ABC that matches the A character or the C character. It is a very practical syntax for a string to stay. The first () found string becomes a $ 1 variable or / 1 variable, the string found in the second () becomes a $ 2 variable or / 2 variable, and it is pushed. / Pattern / I i This parameter indicates that ignore English big lowercase, that is, when matching strings, do not consider the case of English. / If you want to find a special character in Pattern mode, if you want to add / symbol before this character, you will make special characters fail to fail 3, the eight principles of regular expressions If you have used in Unix If these commands, this command, I believe it is not strange to regular expression (Regular Expression). Here are 8 principles from several regular expressions during use. Regular expressions can form a huge alliance in combat against data - this is often a war. We must remember the following eight principles: • Principle 1: Regular expression has three different forms (match (m / / /), replacement (S / / / EG) and conversion (TR / / /)). · Principle 2: Regular expression only matches the scalar ($ scalar = ~ m / A /; can work; @Array = ~ m / A / will treat @Array as scales, so it may not succeed). · Principle 3: Regular expression matches the earliest possible match of a given mode. By default, only regular expressions are matched or replaced ($ A = 'string string2'; $ a = ~ s / string / /; resulting in $ A = 'string 2').

转载请注明原文地址:https://www.9cbs.com/read-81297.html

New Post(0)