This page has been robot translated, sorry for typos if any. Original content here.

# Cheat sheet for regular expressions PERL

On this topic:

## Chapter 6.4.Regular expressions

### 6.4.1.Regular Expression Syntax

Regular expressions are patterns for finding specified combinations of characters in text strings and replacing them with other symbol combinations (these operations are respectively called pattern matching and substitution ). The regular expression in PERL looks like

`  / pattern / modifiers `

Here pattern is a string that specifies a regular expression, and modifiers are optional one-letter modifiers that specify the rules for using this regular expression.

A regular expression can consist of ordinary characters; in this case it will match the specified combination of characters in the string. For example, the expression / cat / corresponds to the selected substrings in the following lines: " cat ok", "for cat ", " cut ". However, the true strength of PERL regular expressions makes it possible to use special metacharacters in them.

Table 6.9. Metacharacters in regular expressions
Symbol Description
\ For characters that are usually treated literally, means that the next character is a metacharacter. For example, / n / matches the letter n, and / \ n / matches the newline character.
For metacharacters, a symbol must be understood literally. For example, / ^ / denotes the beginning of the line, and / \ ^ / corresponds simply to the character ^. / \\ / matches the backslash \.
^ Corresponds to the beginning of the line (compare modifier m ).
\$ Matches the end of the line (compare modifier m ).
. Matches any character except for a line break (compare modifier s ).
* Corresponds to repeating the previous character zero or more times.
+ Corresponds to repeating the previous character one or more times.
? Corresponds to the repetition of the previous character zero or one time.
( pattern ) Corresponds to the pattern line and remembers the match found .
x | y Matches x or y .
{ n } n is a nonnegative number. Matches exactly n occurrences of the previous character.
{ n ,} n is a nonnegative number. Matches n or more occurrences of the previous character. / x {1,} / is equivalent to / x + /. / x {0,} / is equivalent to / x * /.
{ n , m } n and m are nonnegative numbers. Matches at least n and no more than m occurrences of the previous character. / x {0,1} / is equivalent to / x? /.
[ xyz ] Matches any character from the enclosed in square brackets.
[^ xyz ] Matches any character except those enclosed in square brackets.
[ a - z ] Matches any character in the specified range.
[^ a - z ] Matches any character other than those within the specified range.
\ a Matches the bell symbol (BEL).
\ A Matches only the beginning of the line, even with the modifier m .
\ b It corresponds to the word boundary, i.e., the position between \ w and \ W in any order.
\ B Matches any position other than the word boundary.
\ from X Matches the character Ctrl + X. For example, / \ cI / is equivalent to / \ t /.
\ C It corresponds to one byte, even with the use utf8 directive.
\ d Corresponds to the figure. It is equivalent to [0-9].
\ D Matches a non-numeric character. It is equivalent to [^ 0-9].
\ e Matches the escape character (ESC).
\ E The end of the transformations \ L , \ Q , \ U.
\ f Matches the format translation character (FF).
\ G Corresponds to the position in the line equal to pos () .
\ l Converts the next character to lowercase.
\ L Converts characters to lowercase before \ E.
\ n Corresponds to line breaks.
\ p property Matches Unicode characters that have the property property . If the property is specified by several characters, use the \ p { property } syntax.
\ P property Matches Unicode characters that do not have the property property . If the property is specified by several characters, use the syntax \ P { property } .
\ Q Adds the character "\" before the metacharacters to \ E.
\ r Matches a carriage return (CR) character.
\ s Matches the space character. Equivalent to / [\ f \ n \ r \ t] /.
\ S Matches any non-whitespace character. Equivalent to / [^ \ f \ n \ r \ t] /.
\ t Matches the tab character (HT).
\ u Converts the next character to uppercase.
\ U Converts characters to upper case to \ E.
\ w Corresponds to the Latin letter, number or underscore. Equivalent to / [A-Za-z0-9_] /.
\ W Matches any character except for a letter, a number, or an underscore. Equivalent to / [^ A-Za-z0-9_] /.
\ X Corresponds to a sequence of Unicode characters from the main character and a set of diacritic icons. Equivalent to the expression / C <(?: \ PM \ pM *)> /.
\ z Matches only the end of the line, even with the modifier m .
\ Z Corresponds only to the end of the line or the line break at the end of the line, even with the modifier m .
\ n n is a positive number. Corresponds to the nth stored substring . If the left parenthesis before this character is less than n , and n > 9, then it is equivalent to \ 0 n .
\ 0 n n is an octal number not greater than 377. It corresponds to a character with an octal code n . For example, / \ 011 / is equivalent to / \ t /.
\ x n n is a hexadecimal number consisting of two digits. Matches a character with hexadecimal code n . For example, / \ x31 / is equivalent to / 1 /.
\ x { n } n is a hexadecimal number consisting of four digits. Matches a Unicode character with hexadecimal n . For example, / \ x {2663} / is equivalent to / ♣ /.

### 6.4.2.Modifiers

Different operations with regular expressions use different modifiers to specify the operation to be performed. However, the four modifiers have a general purpose.

i
Ignores the case of characters when matching with a pattern. When using the use locale directive, the casting of characters to one register is made taking into account the national setting.
m
Considers the source string as a buffer of several lines of text separated by line breaks. This means that the metacharacters ^ and \$ correspond not only to the beginning and end of the entire string, but to the beginning and end of the line of text bounded by line breaks.
s
Considers the source string as a single line of text, ignoring line breaks. This means that the metacharacter . matches any character, including a line break.
x
Allows the use of spaces and comments. Spaces that do not have a preceding character \ and are not enclosed in [] are ignored. The # symbol starts a comment that is also ignored.

### 6.4.3.Unicode and POSIX character classes

We can use the syntax in regular expressions

`  [: class:] `

where class specifies the name of the POSIX symbol class, that is, the mobile standard for the C language. If you use the utf8 directive, instead of the POSIX class, you can use the Unicode character classes in the construct

`  \ p {class} `

The following table summarizes all POSIX character classes, the corresponding Unicode character classes, and metacharacters, if any.

Table 6.10. Character Classes
POSIX Unicode Metacharacter Description
alpha IsAlpha Letters
alnum IsAlnum Letters and numbers
ascii IsAscii ASCII characters
cntrl IsCntrl Control characters
digit IsDigit \ d Figures
graph IsGraph Letters, numbers and punctuation
lower IsLower Lower case
print IsPrint Letters, numbers, punctuation and space
punct IsPunct Punctuation
space IsSpace \ s Spacing symbols
upper IsUpper Uppercase letters
word IsWord \ w Letters, numbers and underscore

For example, you can specify a decimal number in any of the following three ways:

```  / \ d + /
/ [: digit:] + /
/ \ p {IsDigit} + / # use utf8 ```

To indicate that the symbol does not belong to a given class, constructions are used

```  [: ^ class:]
\ P {class}
```

For example, the following expressions have the same meaning:

```  [: ^ digit:] \ D \ P {IsDigit}
[: ^ space:] \ S \ P {IsSpace}
[: ^ word:] \ W \ P {IsWord} ```

### 6.4.4.Memorizing substrings

The use of parentheses in a regular expression causes the substring corresponding to the pattern in brackets to be stored in a special buffer. To access the nth stored substring within a regular expression, use the construct \ n , and outside it \$ n , where n can take any values ​​starting at 1. However, remember that PERL uses the expressions \ 10 , \ 11 and t etc. as synonyms for the octal codes of the characters \ 010 , \ 011 , etc. Ambiguity here is resolved as follows. The character \ 10 is considered to be a reference to the 10th stored substring, if there are at least ten left parentheses in front of it in the regular expression; otherwise, it is a character with octal code 10. Metacharacters \ 1 , ... \ 9 are always considered as references to stored substrings. Examples:

```  if (/(.)\1/) {# look for the first repeating character
print "'\$ 1' is the first repeating character \ n";
}
if (/ Time: (..)  ..)  ..) /) {# extract the time components
\$ hours = \$ 1;
\$ minutes = \$ 2;
\$ seconds = \$ 3;
} ```

In addition to the variables \$ 1 , \$ 2 , ... there are a few special variables in which the results of the last operation with the regular expression are stored, namely:

Variable Description
\$ & Last found substring.
\$ ` Substring before last found substring.
\$ ' Substring after the last substring found.
\$ + The last stored substring.

Let's give an example:

```  'AAA111BBB222' = ~ / (\ d +) /;
print "\$` \ n ";  # AAA
print "\$ & \ n";  # 111
print "\$ '\ n";  # BBB222
print "\$ + \ n";  # 111 ```

All these special variables retain their values ​​until the end of the enclosing block or until the next successful match with the pattern.

PERL contains several additional constructs that can be used in regular expressions to extend their capabilities. All these constructs are enclosed in parentheses and begin with a symbol ? , which distinguishes them from memorizing substrings.

(? # text )
A comment. The whole construction is ignored.
(? modifiers - modifiers )
Enables or disables the specified modifiers . Modifiers, standing before the symbol - , are included, standing after it - are turned off. Example:
```  if (/ aaa /) {...} # case-sensitive matching
if (/ (i) aaa /) {...} # case-insensitive comparison ```
(?: pattern )
(? modifiers - modifiers : pattern )
Allows you to group subexpressions of a regular expression without remembering the match found. The second form additionally turns on or off the specified modifiers . For example, the expression `/ко(?:т|шка)/` is a short entry of the expression `/кот|кошка/` .
(? = pattern )
Matching with forward looking without memorizing the match found. For example, the expression `/Windows (?=95|98|NT|2000)/` corresponds to "Windows" in the "Windows 98" line, but does not match the "Windows 3.1" line. After matching, the search continues from the position following the match found, without taking into account the forward look.
(?! pattern )
Mismatch with looking ahead without remembering the match found. For example, the expression `/Windows (?!95|98|NT|2000)/` matches "Windows" in the string "Windows 3.1", but does not match in the string "Windows 98". After matching, the search continues from the position following the match found, without taking into account the forward look.
(? <= pattern )
Matching with a look back without memorizing the match found. For example, the expression `/(?<=\t)\w+/` corresponds to the word following the tab character, and the tab character is not included in \$ & . A fragment corresponding to looking back should have a fixed width.
(? <! pattern )
Inconsistency with looking back without remembering the match found. For example, the expression `/(?<!\t)\w+/` corresponds to a word before which there is no tab character. A fragment corresponding to looking back should have a fixed width.

### 6.4.6.Operations with regular expressions

So far we have enclosed regular expressions in the symbols // . In fact, regular expression symbols are defined by the q-operation , which we apply to them. This section describes in detail all the operations of PERL with regular expressions.

#### 6.4.6.1.Comparison with the sample

```  Syntax : / pattern / modifiers m / pattern / modifiers
```

This operation maps the specified string to the pattern pattern and returns true or false, depending on the result of the mapping. The matching string is specified by the left operand of the operation = ~ or ! ~ , For example:

```  \$ mynumber = '12345';
if (\$ mynumber = ~ / ^ \ d + \$ /) {# if the string \$ mynumber consists of decimal digits, then ...
...
} ```

If the string is not specified, then the contents of the special variable \$ _ are compared. In particular, the previous example can be rewritten as follows:

```  \$ _ = '12345';
if (/ ^ \ d + \$ /) {
...
} ```

In addition to the standard, here can be used the following modifiers:

Modifier Description
c Do not reset the search position if the match is unsuccessful (only with the g modifier).
g Global matching, i.e., searching for all occurrences of a pattern.
o Compile the regular expression only once.

If the regular expression is enclosed in // , then the initial m is optional. The construction with the initial m allows us to use as symbols of the regular expression any characters that are allowed in q-operations. Useful special cases:

• If the delimiters are '' , then the pattern is not interpolated. In other cases, the sample interpolates and if it contains variables, then each compilation performs its compilation. To avoid this, use the o modifier (of course, if you are sure that the values ​​of the variables entering the pattern remain unchanged).
• If the limiters are characters ?? , then the single-matching rule applies.

If pattern is an empty string, then the last successfully matched regular expression is used instead.

If the g modifier is not set and the result of the mapping is assigned to the list, an empty list is returned if the mapping is unsuccessful. The result of a successful match depends on the presence of parentheses in the pattern. If they are not present, then the list (1) is returned. Otherwise, a list is returned that consists of the values ​​of the variables \$ 1, \$ 2, etc., ie, a list of all the stored substrings. The following example

`  (\$ w1, \$ w2, \$ rest) = (\$ x = ~ / ^ (\ S +) \ s + (\ S +) \ s*(.*)/); `

puts the first word of the string \$ x in the variable \$ w1 , the second word in the variable \$ w2 , and the rest of this line in the variable \$ rest .

The modifier g enables global matching with the pattern, that is, the search for all matches in the string. Its behavior depends on the context. If the result of the mapping is assigned to the list, then a list of all the stored substrings is returned. If the sample does not contain parentheses, then the list of all matches to the sample is returned, as if it were entirely enclosed in parentheses. The following example

```  \$ _ = "12:23:45";
@result = / \ d + / g;
foreach \$ elem (@result) {
print "\$ elem \ n";
} ```

displays lines 12 , 23 and 45 .

In a scalar context, matching with the g modifier each time looks for the next match to the pattern and returns true or false, depending on the search result. The position in the line after the last match can be read or changed by the pos () function. An unsuccessful search usually resets the search position to zero, but we can avoid this by adding the modifier c . Changing the line also causes the search position to be reset in it.

Additional possibilities are provided by the \ G metacharacter, which only makes sense in conjunction with the g modifier. This metacharacter corresponds to the current search position in the string. The use of the construction m / \ G ... / gc is convenient, in particular, for writing lexical analyzers performing various actions for the tokens encountered in the analyzed text. The following example

```  \$ _ = 'Word1, word2, and 12345.';
LOOP:
{
print ("number"), redo LOOP if /\G\d+\b[,.]?\s*/gc;
print ("word"), redo LOOP if /\ G[A-Za-z0-9]+\b[,.]] ?\s*/gc;
print ("unknown"), redo LOOP if / \ G [^ A-Za-z0-9] + / gc;
} ```

will display the string word word word number .

#### 6.4.6.2.The only comparison with the sample

```  Syntax :?  pattern ?
m?  pattern ? ```

This construction is completely analogous to the m / pattern / construct, with the only difference: a successful match to the pattern is performed only once between calls to the reset () function. This is convenient, for example, when we only need to find the first occurrence of a pattern in each file from the set being viewed, for example:

```  while (<>) {
if (? ^ \$?) {
... # process the first empty line of the file
}
} continue {
reset if eof;  # reset status ??  for the next file
} ```

#### 6.4.6.3.Creating a Regular Expression

```  Syntax : qr / string / modifiers
```

This construct creates a regular expression with string text and modifiers and compiles it. If the delimiters are '' , then interpolation of string is not performed. In other cases, the sample interpolates and if it contains variables, then each compilation performs its compilation. To avoid this, use the o modifier (of course, if you are sure that the values ​​of the variables entering the pattern remain unchanged).

Once created, a regular expression can be used both independently and as a fragment of other regular expressions. Examples:

```  \$ re = qr / \ d + /;
\$ string = ~ / \ s * \$ {re} \ s * /;  # include in another regular expression
\$ string = ~ \$ re;  # independent use
\$ string = ~ / \$ re /;  # same

\$ re = qr / \$ header / is;
s / \$ re / text /;  # same as s / \$ header / text / is ```

#### 6.4.6.4.Substitution

```  Syntax : s / pattern / string / modifiers
```

This operation maps the specified string to the pattern pattern and replaces the found fragments with string . It returns the number of replacements made or false (more precisely, an empty string) if the match fails. The matching string is specified by the left operand of the operation = ~ or ! ~ . It must be a scalar variable, an array element, or an element of an associative array, for example:

```  \$ path = '/ usr / bin / perl';
\$ path = ~ s | / usr / bin | / usr / local / bin |; ```

If the string is not specified, the substitution operation is performed on the special variable \$ _ . In particular, the previous example can be rewritten as follows:

```  \$ _ = '/ usr / bin / perl';
s | / usr / bin | / usr / local / bin |; ```

In addition to the standard, here can be used the following modifiers:

Modifier Description
e Process string as a PERL expression.
g Global substitution, i.e., replacement of all occurrences of the sample.
o Compile the regular expression only once.

We can use any character that is valid in q-operations instead of // . If pattern is enclosed in parentheses, string must have its own pair of delimiters, for example, `s(foo)[bar]` or `s<foo>/bar/` .

If the delimiters are '' , then the pattern is not interpolated. In other cases, the sample interpolates and if it contains variables, then each compilation performs its compilation. To avoid this, use the o modifier (of course, if you are sure that the values ​​of the variables entering the pattern remain unchanged).

If pattern is an empty string, then the last successfully matched regular expression is used instead.

By default, only the first sample found is replaced. To replace all occurrences of a pattern in a string, you must use the g modifier.

The e modifier indicates that string is an expression. In this case, the function eval () is first applied to string , and then the substitution is performed. Example:

```  \$ _ = '123';
s / \ d + / \$ & * 2 / e;  # \$ _ = '246'
s / \ d / \$ & * 2 / eg;  # same ```

Here are some more typical examples of using a substitution operation. Removing comments of the form / * ... * / from the text of a Java or C program:

```  \$ program = ~ s {
/ \ * # Beginning of comment
. *?  # The minimum number of characters
\ * / # End of comment
} [] gsx; ```

Delete the leading and trailing spaces in the \$ var line:

```  for (\$ var) {
s / ^ \ s + //;
s / \ s + \$ //;
} ```

Permutation of the first two fields in \$ _ . Note that the replacement line uses the variables \$ 1 and \$ 2 , not the metacharacters \ 1 and \ 2 :

`  s / ([^] *) * ([^] *) / \$ 2 \$ 1 /; `

Replace tabs with spaces with alignment on columns that are multiples of eight:

`  1 while s / \ t + / '' x (length (\$ &) * 8 - length (\$ `)% 8) / e; `

#### 6.4.6.5.Transliteration

```  Syntax : tr / list1 / list2 / modifiers y / list1 / list2 / modifiers
```

Transliteration consists in replacing all the characters from the list list1 with the corresponding characters from the list2 list. It returns the number of replaced or deleted characters. Lists must consist of separate characters and / or ranges of the form az . The string to be converted is given by the left operand of the operation = ~ or ! ~ . It must be a scalar variable, an array element, or an element of an associative array, for example:

```  \$ test = 'ABCDEabcde';
\$ test = ~ tr / AZ / az /;  # Replace lowercase letters with uppercase letters ```

If the string is not specified, the substitution operation is performed on the special variable \$ _ . In particular, the previous example can be rewritten as follows:

```  \$ _ = 'ABCDEabcde';
tr (AZ) az); ```

We can use any character that is valid in q-operations instead of // . If list1 is enclosed in parentheses, then list2 must have its own pair of delimiters, for example `tr(AZ)[az]` or `tr<AZ>/az/` .

Usually this operation is called tr . The synonym y is introduced for fanatics of the sed editor and is used only by them. Transliteration supports the following modifiers:

Modifier Description
c Replace characters not included in list1 .
d Delete characters for which there is no replacement.
s Delete duplicate characters when replacing.
U Convert to / from the UTF-8 encoding.
C Convert to / from a single-byte encoding.

The c modifier causes transliteration of all characters that are not in the list1 list. For example, the operation `tr/a-zA-Z/ /c` will replace all non-Latin letters with spaces.

By default, if list2 is shorter than list1 , it is updated with its last character, and if it is empty, it is assumed to be equal to list1 (this is convenient for counting the number of characters of a certain class in a string). Modifier d modifies these rules: all characters from list1 that do not match in list2 are removed from the string. For example, the operation `tr/a-zA-Z//cd` will remove from the string all characters that are not Latin letters.

The s modifier removes duplicates: if several characters are successively replaced by the same character, only one instance of this symbol will be left. For example, the operation `tr/ / /s` removes repeated spaces in the string.

Modifiers C and U are designed for character conversion from system encoding to UTF-8 and vice versa. The first one points to the original encoding, and the second to the encoding of the result. For example, `tr/\0-\xFF//CU` recode the string from the system encoding to UTF-8, and `tr/\0-\xFF//UC` will perform the reverse transcoding.

Transliteration is done without interpolating symbol lists, so to use the variables in it, you need to call the eval () function, for example:

`  eval "tr / \$ oldlist / \$ newlist /"; `