Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Perl regular expressions

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use Perl regular expressions, I hope you will learn something after reading this article, let's discuss it together!

Perl regular expression

Perl is widely used in IC design, and regular expressions of Perl are common, which are similar to regular expressions in Tcl and other languages, so it will not be very difficult if you have learned about regular expressions.

Three forms of regular expressions

First of all, we should know that there are three forms of regular expressions in Perl programs, which are:

Match: mCompare / (can also be abbreviated to / /, omitting m)

Replace: sUnix /

Converting: tr///

These three forms are generally used with = ~ or! ~, and the scalar variable to be processed on the left. If there is no such variable and the = ~! ~ operator, the default is to process the contents of the $_ variable.

(where "= ~" means match, read as does in the whole sentence, "! ~" means mismatch, and read as doesn't in the whole sentence)

Examples are as follows:

$str= "I lovePerl"

$str=~m/Perl/;# means that if the string "Perl" is found in $str, it returns "1" otherwise returns "0".

Str=~s/Perl/BASH/;# means to replace the "Perl" string in the variable $str with "BASH". If this replacement occurs, it returns "1", otherwise it returns "0".

$stringed letters tracker, Amurz str, Amurz etc. # means to convert all uppercase letters in the variable $string to lowercase letters, and to return "0" if the conversion occurs, or "1" otherwise.

There are also:

Foreach (@ array) {sqqr _ b _;} # where each loop takes an element from the @ array array and stores it in the $_ variable, and replaces the $_.

While () {print if (m/error/);} # is a little more complicated, printing all lines in the FILE file that contain error strings.

If () appears in the regular expression of Perl, the pattern in () is automatically assigned to the system $1 by the Perl interpreter after matching or replacing.

Take a look at the following example:

$string= "Ilove perl"

$string=~s/ (love) / /; # at this point $1 = "love", and the result of this replacement is to change $string to "Iperl"

$string= "ilove perl"

$string=~s/ (I) (. *) (perl) / $2Universe # where $1 = "I", $2 = "love", $3 = "perl", and $string becomes "love" after replacement

1. Pattern matching

1.1, matching operator

The matching operator m _ run / is used to match a string statement or a regular expression, for example, to match "run" in the scalar $string, the code is as follows:

Example

#! / usr/bin/perl

Bar = "I am runoob site. Welcome to runoob site."

If ($bar = ~ / run/) {

Print "first match\ n"

} else {

Print "first mismatch\ n"

}

$bar = "run"

If ($bar = ~ / run/) {

Print "second match\ n"

} else {

Print "second mismatch\ n"

}

Execute the above program, and the output is as follows:

The first match

The second match

1.2. Pattern matching modifier

Pattern matching has some common modifiers, as shown in the following table:

Modifier

Description

I

Ignore case in mode

M

Multiline mode

O

Assign a value only once

S

Single line mode, "." Match "\ n" (does not match by default)

X

Ignore whitespace in the pattern

G

Global matching

Cg

After a global match fails, the matching string is allowed to be found again.

1.3, regular expression variables

After perl processing, there are three special variable names for the matched values:

$`: the string that matches the first part of the part

$&: matching string

$': there is no remaining string that matches

If you put these three variables together, you will get the original string.

Examples are as follows:

Example

#! / usr/bin/perl

$string = "welcome to runoob site."

$string = ~ m/run/

Print "string before matching: $`\ n"

Print "matching string: $&\ n"

Print "matched string: $'\ n"

The output result of executing the above program is:

String before matching: welcome to

Matching string: run

The matched string: oobsite.

2. Mode replacement

2.1. Replace operator

The replacement operator sswap / is an extension of the matching operator, replacing the specified string with a new string. The basic format is as follows:

S/PATTERN/REPLACEMENT/

PATTERN is the matching pattern and REPLACEMENT is the replacement string.

For example, we replace the "google" of the following string with "runoob":

Example

#! / usr/bin/perl

$string = "welcome to google site."

$string = ~ s/google/runoob/

Print "$string\ n"

The output result of executing the above program is:

Welcome to runoob site.

2.2. Replace operation modifier

The replacement operation modifier is shown in the following table:

Modifier

Description

I

If you add "I" to the modifier, the regular will remove case sensitivity, that is, "a" and "A" are the same.

M

The default regular start "^" and end "$" is only for regular strings if you add "m" to the modifier, then the beginning and end of each line will refer to each line of the string: each line begins with "^" and ends with "$".

O

The expression is executed only once.

S

If you add "s" to the modifier, the default is "." Means that any character other than a newline character will become any character, including a newline character!

X

If you add this modifier, the white space character in the expression will be ignored unless it has been escaped.

G

Replace all matching strings.

E

Replace the string as an expression

For example:

SpicActionAccord G means to replace all matching patterns in the string to be processed with strings.

SqqameUniPure e indicates that the part will be treated as an operator. This parameter is not used much.

For example, the following example:

$string= "i:love:perl"

$string=~s/:/*/;# now $string= "i*love:perl"

$string=~s/:/*/g;# now $string= "i*love*perl"

$string=~tr/*//;# now $string= "iloveperl"

$string= "www22cgi44"

$string=~s/ (\ d +) / $1 string 2 * string * (/ d +) represents one or more numeric characters in $string, and performs the operation of * 2 on these numeric characters, so finally $character becomes "www44cgi88".

3. Mode transformation

This is another alternative, syntax such as: tr/string1/string2/. Again, string2 is the replacement part, but the effect is to replace the first character in string1 with the first character in string2, the second character in string1 with the second character in string2, and so on. Such as:

$string = "abcdefghicba"

$string = ~ tr/abc/def/; # now string = "defdefghifed"

When string1 is longer than string2, its extra characters are replaced with the last character of string2; when the same character appears more than once in string1, the first replacement character is used.

For example:

#! / usr/bin/perl

$str = "abbcddeff"

$str = ~ tr/abcc/ABCD/

Print "$str\ n"

Output:

ABBCddeff

3.1. Conversion operator

The following are the modifiers related to the conversion operator:

Modifier

Description

C

Convert all unspecified characters

D

Delete all specified characters

S

Reduce multiple identical output characters to one

The following example converts all lowercase letters in the variable $string to uppercase letters:

#! / usr/bin/perl

$string = 'welcome to runoob site.'

$string = ~ tr/a-z/A-Z/

Print "$string\ n"

The output result of executing the above program is:

WELCOME TO RUNOOB SITE.

The following example uses / s to remove the duplicate characters of the variable $string:

Example

#! / usr/bin/perl

$string = 'runoob'

$string = ~ tr/a-z/a-z/s

Print "$string\ n"

The output result of executing the above program is:

Runob

More examples:

$string = ~ tr/\ d / / c; # replace all non-numeric characters with spaces

$string = ~ tr/\ t / / d; # Delete tab and spaces

$string = ~ tr/0-9 / / cs # replace the other characters between the numbers with a space.

A common pattern in regular expressions.

/ pattern/ result

. Matches all characters except newline characters

X? Match 0 or once x strings

X * match 0 or more x strings, but the least number of times possible

X + matches the x string one or more times, but as many times as possible

. * any character that matches 0 or more times

. + any character that matches one or more times

X {m} matches the specified string of m x

X {m direction n} matches specified strings greater than or equal to m and less than or equal to n x

X {m,} matches specified strings greater than or equal to m x

[] matches the characters in []

[^] match does not match the characters in []

[0-9] matches all numeric characters

[amurz] matches all lowercase characters

[^ 0-9] matches all non-numeric characters

[^ aMuz] matches all non-lowercase alphabetic characters

^ matches the character at the beginning of the character

$matches the character at the end of the character

\ d matches the character of a number, the same as the [0-9] syntax

\ d + matches multiple numeric strings, same as [0-9] + syntax

\ d is not a number, others are the same as\ d

\ D+ is not numeric, others are the same as\ d+

\ w A string of letters or numbers in English, the same as the [a-zA-Z0-9] syntax

\ W + is the same as [a-zA-Z0-9] + syntax

\ W A string that is not alphabetic or numeric, as in [^ a-zA-Z0-9] syntax

\ W+ is the same as [^ a-zA-Z0-9] + syntax

\ s space, same as [\ n\ t\ r\ f] syntax

\ s + is the same as [\ n\ t\ r\ f] +

\ s is not a space, same as [^\ n\ t\ r\ f] syntax

\ S+ is the same as [^\ n\ t\ r\ f] + syntax

\ b matches strings bounded by letters and numbers

\ B matches strings that are not bounded by letters and values

A | b | c matches strings that match a character, b character or c character

Abc matches a string containing abc

(pattern) () this symbol remembers the string you are looking for and is a very useful syntax. The string found in the first () becomes the variable $1 or\ 1, the string found in the second () becomes the variable $2 or\ 2, and so on.

The parameter / pattern/i I indicates that English case is ignored, that is, when matching strings

After reading this article, I believe you have some understanding of "how to use Perl regular expressions". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report