ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
7.2 Simple Uses of Regular ExpressionsIf we were looking for all lines of a file that contain the string grep abc somefile >results In this case, In Perl, we can speak of the string if (/abc/) { print $_; } But what is being tested against the regular expression For this example, the while (<>) { if (/abc/) { print $_; } } What if we didn't know the number of grep "ab*c" somefile >results (The argument containing the asterisk is in quotes because we don't want the shell expanding that argument as if it were a filename wildcard. It has to be passed as-is to grep to be effective.) In Perl, we can say exactly the same thing: while (<>) { if (/ab*c/) { print $_; } } Just like grep, this means an We'll visit more uses of pattern matching in Section 7.4, "More on the Matching Operator," later in the chapter, after we talk about all kinds of regular expressions. Another simple regular expression operator is the substitute operator, which replaces the part of a string that matches the regular expression with another string. The substitute operator looks like the s/ab*c/def/; The variable (in this case, As with the match operator, we'll revisit the myriad options on the substitute operator later, in Section 7.5, "Substitutions." |