Developer's Lab

By Diogo Pinto - DiØ

Capturing Groups Regex With Perl

Capturing groups in regular expression, against a scalar type, it’s possible when you use the variable number, such as $1, $2, $3…, for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env perl

print "Enter your name and lastname here => ";
chop ( $name_and_lastname = <STDIN> );

if ( $name_and_lastname =~ /^\s*(\S+)\s+(\S+)\s*$/ )
{
  print "Hello $1. Your lastname: $2.";
}
else
{
  print "no result";
}
print "\n";

output:

Comments