Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does the regex like ".*" work in perl

Tags:

regex

perl

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 /.

like image 480
yQuery Avatar asked Oct 25 '13 07:10

yQuery


People also ask

What is a Perl regular expression?

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.

What is the use of negated regex in Perl?

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 (=~)

What are metacharacters in Perl regular expressions?

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.

What is the use of =~ in Perl?

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.


2 Answers

.* matches two times. First, it matches "scott". Then it matches the empty string after "scott".

like image 147
ysth Avatar answered Oct 25 '22 23:10

ysth


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.

like image 28
ikegami Avatar answered Oct 25 '22 21:10

ikegami