Regular expression learning notes

zhaozj2021-02-17  80

1. Three forms of regular expressions First, we should know that in the Perl program, there are three forms of existence, they are:

Match: m / / (can also be short-handed / /, slightly M)

Replace: S / / /

Transformation: Tr / / /

These three forms are generally used with = ~ or! ~ Match (where "= ~" represents matching, reading as DOES in the entire statement, "! ~" Means that do not match, read in whole statement in doesn ' T) and have the scalar variable to be processed on the left. If there is no such variable and = ~! ~ Operator, the contents of the $ _ variable will be derived by default. Moreover:

Foreach (@Array) {S / A / B /;} # The loop from the @Array array is stored in the $ _ variable each time a loop is stored in the $ _ variable and replaces 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 S / / / can also add E or G parameters at the end, and their meaning is:

S / / / g represents all the models of all the in a string to be processed into the string instead of replacing 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 have to 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').

· Principle 4: Regular expression can handle any and all characters that can be processed by dual quotes ($ A = ~ m / $ varb / extension Varb to variables before matching; if $ varb = 'a' $ a = 'AS ', $ A = ~ s / $ varb / /; equivalent to $ a = ~ s / A / /; execution results make $ a = "s"). · Principles 5: Regular expressions Generate two situations in the evaluation process: result status and reverse reference: $ a = ~ m / pattern / indicate if there is a bunch pattern in $ A, $ A = ~ s / ( Word1) (Word2) / $ 2 $ 1 / "Convert" two words.

· Principle 6: The core capability of regular expressions is that wildcards and multiple matching operators and how they operate. $ A = ~ m // W / Match one or more word characters; $ a = ~ m // D / "matches zero or more numbers.

· Principle 7: If you want to match more than one character set, Perl uses "|" to increase flexibility. If you enter M / (Cat | DOG) /, it is equivalent to "matching string CAT or DOG.

· Principle 8: Perl Use (? ..) syntax to provide extended features to regular expressions.

(Want to learn all these principles? I suggest you first start, and constantly try and experiment.

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

New Post(0)