Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does $1 mean in Perl?

Tags:

regex

perl

What does $1 mean in Perl? Further, what does $2 mean? How many $number variables are there?

like image 445
Chad DeShon Avatar asked Jun 24 '09 03:06

Chad DeShon


People also ask

What is the meaning of $1 in Perl regex?

$1 equals the text " brown ".

What does$ 1 mean in regex?

For example, the replacement pattern $1 indicates that the matched substring is to be replaced by the first captured group.

What is $_ in Perl?

The most commonly used special variable is $_, which contains the default input and pattern-searching string. For example, in the following lines − #!/usr/bin/perl foreach ('hickory','dickory','doc') { print $_; print "\n"; }

What does =~ in Perl?

9.3. The Binding Operator, =~ Matching against $_ is merely the default; the binding operator (=~) tells Perl to match the pattern on the right against the string on the left, instead of matching against $_.


5 Answers

The $number variables contain the parts of the string that matched the capture groups ( ... ) in the pattern for your last regex match if the match was successful.

For example, take the following string:

$text = "the quick brown fox jumps over the lazy dog.";

After the statement

$text =~ m/ (b.+?) /;

$1 equals the text "brown".

like image 75
rlbond Avatar answered Oct 21 '22 11:10

rlbond


The number variables are the matches from the last successful match or substitution operator you applied:

my $string = 'abcdefghi';

if ($string =~ /(abc)def(ghi)/) {
    print "I found $1 and $2\n";
}

Always test that the match or substitution was successful before using $1 and so on. Otherwise, you might pick up the leftovers from another operation.

Perl regular expressions are documented in perlre.

like image 31
Jim Puls Avatar answered Oct 21 '22 12:10

Jim Puls


$1, $2, etc will contain the value of captures from the last successful match - it's important to check whether the match succeeded before accessing them, i.e.

 if ( $var =~ m/( )/ ) { # use $1 etc... }

An example of the problem - $1 contains 'Quick' in both print statements below:

#!/usr/bin/perl

'Quick brown fox' =~ m{ ( quick ) }ix;
print "Found: $1\n";

'Lazy dog' =~ m{ ( quick ) }ix;
print "Found: $1\n";
like image 12
plusplus Avatar answered Oct 21 '22 13:10

plusplus


As others have pointed out, the $x are capture variables for regular expressions, allowing you to reference sections of a matched pattern.

Perl also supports named captures which might be easier for humans to remember in some cases.

Given input: 111 222

/(\d+)\s+(\d+)/

$1 is 111

$2 is 222

One could also say:

/(?<myvara>\d+)\s+(?<myvarb>\d+)/

$+{myvara} is 111

$+{myvarb} is 222

like image 10
Einstein Avatar answered Oct 21 '22 13:10

Einstein


These are called "match variables". As previously mentioned they contain the text from your last regular expression match.

More information is in Essential Perl. (Ctrl + F for 'Match Variables' to find the corresponding section.)

like image 6
John T Avatar answered Oct 21 '22 13:10

John T