Perl Regular ExpressionA 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:
Perl Matching OperatorsPerl matching operators have various modifiers. It is mainly used to match a string or statement to a regular expression. Matching Operator Modifiers
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 mThe 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 OperatorThe 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 OperatorTranslation 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 letterHere, 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 letterHere, 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 Next TopicPerl split Function |