Javatpoint Logo
Javatpoint Logo

Perl Regular Expression

A regular expression is a string of characters that defines a specific pattern. The perl regular expression syntax is quite similar with that of awk, grep and sed.

There are three regular expression operators inside perl:

  • Matching regular expression operator
  • Substitute regular expression operator
  • Transliterate regular expression operator

Perl Matching Operators

Perl matching operators have various modifiers. It is mainly used to match a string or statement to a regular expression.


Matching Operator Modifiers

Operators Description
cg Continue search even if the global match fails
g Search globally for all matches
i Search the match with case insensitivity
m If string has a new line character, the $ and ^ will match against a new line boundary instead of string boundary
o Allow expression evaluation only once
s Use . to match a new line character
x Use white space in the expression

Perl Matching Operator =~

The matching operator =~ is used to match a word in the given string. It is case sensitive, means if string has a lowercase letter and you are searching for an uppercase letter then it will not match.

Output:

Matching
Not Matching

Perl Matching Operator !~

It is the opposite of the earlier one (=~). If the letters match it gives the output as not matched and vice versa.

Output:

Not Matching
Matching

Perl Matching Operator with $_

You can also match it against a special default variable $_.

Output:

Matching
Not Matching

Perl Matching Operator with m

The matching operator m is also used to match a word in the given string.

Output:

Matching
Not Matching

Perl Matching Operator with $1, $2...

The $1, $2 will print the word according to the specified bracket.

Output:

1: CuNaHg 2: CuNa 3: Cu 4: Na 5: Hg 6: 

Perl Matching Operator with ?

It prints the matched character inside the bracket from a given string.

Output:

Cu
Na
Hg 

Perl Substitution Operator

The substitution operator is just an extension of the matched operator. It allows the replacement of text matched with some new text.

Its basic syntax is:


Perl Substitution Operator with s///

Here we are replacing liquid with solid in the first part with s///.

In the second part, 'liquid' is replaced with 'solid' globally with s///g.

Output:

solid will remain liquid until it is evaporated
solid will remain solid until it is evaporated

Perl Translation Operator

Translation operator is similar as substitution operator. But translation does not use regular expression for search on replacement values.

Its basic syntax is:


Perl Translation Operator replacing one letter

Here, all the 'l' letters will be replaced with 'z' letters by translation operator.

Output:

ziquid wizz remain ziquid untiz it is evaporated

Perl Translation Operator replacing more than one letter

Here, all the 'l' and 'i' letters will be replaced with 'z' and 'x' letters by translation operator.

Output:

zxquxd wxzz remaxn zxquxd untxz xt xs evaporated




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA