I am puzzled by regular expression like .*
#!usr/bin/perl
use strict;
use warnings;
my $text = "scott";
$text =~ s/.*/\//g;
print $text;
Output: //
So, I don't know why the result is //
rather than /
.
Summary: in this tutorial, you are going to learn about Perl regular expression, the most powerful feature of the Perl programming language. A regular expression is a pattern that provides a flexible and concise means to match the string of text. A regular expression is also referred to as regex or regexp.
In Perl, regular expression negated regex operator is used to checking the string is not equal to the regular expression which was specified on the right-hand side. Below is the example are as follows. Example #1. Perl matching operator (=~)
Up to now you’ve noticed that the regular expression engine treats some characters in a special way. These characters are called metacharacters. The following are the metacharacters in Perl regular expressions: {} [] ()^$.|*+?\ To match the literal version of those characters, you have to a backslash \ in front of them in the regular expressions.
Code language: Perl (perl) The operator =~ is the binding operator. The whole expression returns a value to indicate whether the regular expression regex was able to match the string successfully. Let’s take a look at an example.
.*
matches two times. First, it matches "scott". Then it matches the empty string after "scott".
The first time through, .*
matches 5 characters starting at position zero (scott
). This gets replaced with /
.
/g
means match as many times as possible, so it tries again, this time start at position five.
The second time through, .*
matches 0 characters starting at position five (empty string). This gets replaced with /
.
The third time through, .*
goes to matches 0 characters starting at position five (""), but there's a check that makes sure it doesn't match the same starting position and length twice in a row. So it advances the position and tries to match at position six. Since that's outside the string it fails.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With