Perl regular expression

Regular expression describes a pattern of string matching that may be used to check if a string consists of a substring, update an identical substring, or take a string from a circumstance. Substrings, and many others.

Perl's ordinary expressions are very effective and basically the maximum powerful of the typically used languages. Many language design ordinary styles assist Perl's normal expressions.

The 3 varieties of Perl's normal expression are healthy, replace, and rework:

  • Match: m// (also can be abbreviated as //, o m)

  • Replace: s///

  • Conversion: tr///

These 3 paperwork are commonly used together with =~ or !~, =~ means suit, !~ manner no healthy.

in shape operator

The in shape operator m// is used to in shape a string statement or a ordinary expression, for instance, to suit "run" in the scalar $bar, the code looks like this:


#!/usr/bin/perl $bar= " I am welookups web page. Welcome to welookups website online."; if ($bar =~ /run/) print "First healthyn"; else print "First mismatchn"; $bar= " run"; if ($bar =~ /run/) print "Second fitn"; else print "Second time mismatchn";

Execute the above program, the output is:

First match
Second match

Mode matching modifier

Pattern matching has some not unusual modifiers, as shown in the following desk:

iIgnore case in mode
m Multiline mode
oAssign only once
sSingle line mode, "." fits "n" (default does now not fit)
xIgnore blanks inside the sample
gGlobal Match
cgAfter the global healthy fails, it's far allowed to find the matching string once more

regular expression variable

There are 3 unique variable names for the matched values after perl is processed:

  • $`: suits the first a part of the string
  • $&: matching string
  • $': There are not any matching last strings

If you put those 3 variables together, you may get the original string.

Examples are as follows:


#!/usr/bin/perl $string = " welcome to welookups web page."; $string =~ m/run/; print "The string earlier than the in shape: $`n "; print "matched string: $&n"; print "Matched strings: $'n ";

Execute the above application output:

String earlier than matching: welcome to
Matching strings: run
The matched string: oob web page.

Replace operator

The replacement operator s/// is an extension of the suit operator, changing the specified string with a brand new one. The primary layout is as follows:


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

For instance, we update "google" with the following string "welookups":


#!/usr/bin/perl $string = " welcome to google web site."; $string =~ s/google/welookups/; print "$stringn";

Execute the above software output:

welcome to welookups website online.

Replace operation modifier

The replacement movement modifier is proven within the following table:

iIf you add "i" to the modifier, the everyday will cancel the case sensitivity, ie "a" and "A" are the identical.
mThe default normal begin "^" and end "$" are just for ordinary strings. If you upload "m" to the modifier, the start and stop will seek advice from the characters. Each line of the string: the beginning of each line is "^" and the give up is "$".
o expression is most effective finished once.
sIf you upload "s" to the modifier, the default "." means that any character aside from the newline turns into any character, consisting of line breaks. !
x If this modifier is delivered, whitespace characters inside the expression could be neglected until it has been escaped.
gReplace all matching strings.
eReplace a string as an expression

Conversion operator

The following are the modifiers related to the conversion operator:

cConvert all unspecified characters
dDelete all certain characters
sReduce more than one same output characters into one

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


$string = 'welcome to welookups website.' ;
$string =~ tr/a-z/A-z/;

print "$stringn";

Execute the above software output:

WELCOME TO welookups SITE.

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


#!/usr/bin/perl $string = ' welookups'; $string =~ tr/a-z/a-z/s; print "$stringn";

Execute the above program output:


More examples:

$string =~ tr/d / /c; # Replace all non-numeric characters with spaces
$string =~ tr/t //d; # Delete tabs and spaces
$string =~ tr/0-nine/ /cs # The character is changed with a space. 

More ordinary expression guidelines

Expression Description
fit all characters besides newline
x? fits zero instances or once x string
x* fits zero or extra x strings, however fits the least viable range of times
x+ suits 1 or greater x strings, however suits the least viable wide variety of instances
.* matches any person 0 or greater instances
.+ Matches any character 1 or extra times
m fits precisely the required string of m
m,n fits the desired string of m or extra and n or much less
m, fits extra than m distinct strings
[] Matches the characters in []
[^] matches do now not healthy the characters in []
[0-9] fits all numeric characters
[a-z] fits all lowercase alphabetic characters
[^0-9] suits all non-numeric characters
[^a-z] suits all non-lowercase alphabetic characters
^ suits the individual at the start of the man or woman
$ Matches characters at the give up of a person
d matches a number of characters, similar to [0-9]
d+ fits a couple of numeric strings, the same as [0-9]+ syntax
D Non-digit, other with d
D+ Non-digit, the same as d+
w A string of English letters or numbers, same as the [a-zA-Z0-9_] syntax
w+ is the same as [a-zA-Z0-9_]+
W A string of non-English letters or numbers, same as the [^a-zA-Z0-9_] syntax
W+ is similar to [^a-zA-Z0-9_]+
s Space, same as [ntrf]
s+ Same as [ntrf]+
S Non-space, same as [^ntrf]
S+ is similar to [^ntrf]+
b Matches strings with English letters and numbers as borders
B suit strings that aren'tc
Abc suits a string containing abc (pattern) () This symbol recollects the string it reveals. It is a very beneficial syntax. The string discovered inside the first () will become $1 or the 1 variable, the second ( The string found internal turns into the $2 variable or the 2 variable, and so forth.
/sample/i i This parameter shows that English case is unnoticed, this is, whilst matching strings, the case of English is not considered. If you need to discover a special man or woman in the sample mode, such as "*", you need to placed a image in the front of this character, in order that unique characters could be invalidated.

welookups is optimized for learning.© welookups. 2018 - 2020 All Right Reserved and you agree to have read and accepted our term and condition.