I am new to perl. Can anyone explain the meaning of the following line of code:
我是perl新手。谁能解释下面一行代码的含义:
my ($H,$M,$S) = $date =~ m{^([0-9]{2}):([0-9]{2}):([0-9]{2})}
I assume that after the execution of this line $H
, $M
and $S
will have the values extracted from $date
. Can anyone explain to get a better understanding?
我假设在执行这一行$H之后,$M和$S将提取$date中的值。谁能解释一下以便更好地理解?
3 个解决方案
#1
8
It tries to match the contents of the $date
variable, with a regex:
它试图匹配$date变量的内容,并使用regex:
^([0-9]{2}):([0-9]{2}):([0-9]{2})
The regex basically means: from the start of the string, there should be two digits and colons repeated three times. Each of these three two digit numbers are enclosed in a group.
regex的基本意思是:从字符串开始,应该有两个数字和冒号重复三次。这三个两位数中的每一个都包含在一个组中。
Finally, the matches of the three groups are assigned to local variables $H
, $M
and $S
.
最后,将这三组的匹配分配给本地变量$H、$M和$S。
For example if
$date = "10:37:21 2016.01.02";
then
然后
$H = "10";
$M = "37";
$S = "21";
#2
1
Can anyone explain to get a better understanding?
谁能解释一下以便更好地理解?
You need to start to be aware of two things:
你需要开始意识到两件事:
-
list context
上下文列表
-
scalar context
标量上下文
The match operator, m//
, will provide different results depending on what's on the left hand side of your =
sign. Check this out:
匹配操作符m// /将根据=符号的左边提供不同的结果。看看这个:
use strict;
use warnings;
use 5.020;
my $result = "abc" =~ m/a(.)(.)/;
say $result; #=> 1
my @results = "abc" =~ m/a(.)(.)/;
for my $result (@results) {
say $result;
};
--output:--
b
c
A $variable
can only store one thing, so when there is a $variable on the left hand side of the =
sign, the $variable looks over to the match operator, m//
, on the right hand side of the =
sign and calls out, "Hey, I can only store one thing over here, just give me one thing, please!" The match operator responds by returning 1, for true, if there was a match; or 0, for false, if there wasn't a match.
美元变量只能存储一件事,所以,当有一个美元变量放在等号的左边,$变量看起来匹配算子,m / / =右边的标志和电话,“嘿,我只能存储一件事,给我一件事,请!”匹配操作符的响应是返回1,如果存在匹配,则为true;如果没有匹配,则为0。
On the other hand, when an @variable
is on the left hand side of the =
sign, the array looks over to the m//
operator and calls out, "Hey, I can store a bunch of things over here, so give me a bunch of stuff, please!" The match operator responds by returning what matched the capture groups in the regex if there was a match; if there wasn't a match, the match operator returns ()
.
另一方面,当@变量在=符号的左边时,数组会向m//运算符查找,并调用:“嘿,我可以在这里存储很多东西,所以请给我一堆东西!”匹配操作符通过返回匹配regex中的捕获组的内容作为响应;如果没有匹配,匹配操作符返回()。
In the first case, the $variable
is said to provide scalar context
for the match operator. In the second case, the @variable
is said to provide list context
for the match operator. Don't let those terms scare you. You know what they mean now.
在第一种情况下,$变量被称为为match操作符提供标量上下文。在第二种情况下,@variable被称为为match操作符提供列表上下文。不要让这些术语吓到你。你知道他们现在的意思。
Next, when you write this:
接下来,当你写下这些:
my ($H,$M,$S) =
You are creating several variables on the left hand side of the =
sign. In unison, they call out to the match operator on the other side of the =
sign, "Hey, there are many of us over here, give us the bunch of stuff, please! That particular my
syntax provides a list context
for the match operator which is on the right hand side of the =
sign:
您正在=符号的左边创建几个变量。他们异口同声地对=号另一边的报务员喊道:“嘿,这儿有很多人,请把这些东西给我们!”这个特殊的my语法为match操作符提供了一个列表上下文,它位于=号的右边:
my ($group1, $group2) = "abc" =~ m/a(.)(.)/;
say $group1; #=> b
say $group2; #=> c
Note that if the delimiters you use for the match operator are m/.../
, then you don't have to write the leading m
, so typically you will see the example above written as:
注意,如果您用于匹配操作符的分隔符是m/…/,这样你就不用写前导m了,所以通常你会看到上面的例子是这样写的:
my ($group1, $group2) = "abc" =~ /a(.)(.)/;
When you use braces like you did: m{...}{...}
, then you have to write the leading m
.
当你使用像你这样的牙套时:……},然后写出前导m。
#3
0
You can use a simpler regex, which is easier to understand, to do what you want:
你可以使用更简单的正则表达式,更容易理解,做你想做的:
\d{2} #\d means a digit, {2} means twice,
#so this matches two consecutive digits
Here's how you can use that regex:
以下是如何使用regex:
#Just blindly use all three of these in every program:
use strict;
use warnings;
use 5.020;
my $date = "10:37:21 2016.01.02";
my ($H,$M,$S) = $date =~ /\d{2}/g; #g => global, Find all matches in the string
say $H; #say() is the same as print() with a newline at the end
say $M;
say $S;
--output:--
10
37
21
The regex starts at the beginning of the string and looks for two consecutive digits and finds 10, so that is a match; then the regex jumps over the :
and finds 37, so that is a match; then the regex jumps over the :
and finds 21, so that is a match; etc., etc.
regex从字符串的开始处开始,查找两个连续的数字,然后找到10,所以这是一个匹配;然后regex跳过:并找到37,因此这是一个匹配;然后regex跳过:并找到21,因此这是一个匹配;等等,等等。
When you assign all the matches to three variables, the first three matches are assigned to the three variables, and the rest of the matches are discarded.
当您将所有匹配分配给三个变量时,前三个匹配被分配给这三个变量,其余的匹配被丢弃。
#1
8
It tries to match the contents of the $date
variable, with a regex:
它试图匹配$date变量的内容,并使用regex:
^([0-9]{2}):([0-9]{2}):([0-9]{2})
The regex basically means: from the start of the string, there should be two digits and colons repeated three times. Each of these three two digit numbers are enclosed in a group.
regex的基本意思是:从字符串开始,应该有两个数字和冒号重复三次。这三个两位数中的每一个都包含在一个组中。
Finally, the matches of the three groups are assigned to local variables $H
, $M
and $S
.
最后,将这三组的匹配分配给本地变量$H、$M和$S。
For example if
$date = "10:37:21 2016.01.02";
then
然后
$H = "10";
$M = "37";
$S = "21";
#2
1
Can anyone explain to get a better understanding?
谁能解释一下以便更好地理解?
You need to start to be aware of two things:
你需要开始意识到两件事:
-
list context
上下文列表
-
scalar context
标量上下文
The match operator, m//
, will provide different results depending on what's on the left hand side of your =
sign. Check this out:
匹配操作符m// /将根据=符号的左边提供不同的结果。看看这个:
use strict;
use warnings;
use 5.020;
my $result = "abc" =~ m/a(.)(.)/;
say $result; #=> 1
my @results = "abc" =~ m/a(.)(.)/;
for my $result (@results) {
say $result;
};
--output:--
b
c
A $variable
can only store one thing, so when there is a $variable on the left hand side of the =
sign, the $variable looks over to the match operator, m//
, on the right hand side of the =
sign and calls out, "Hey, I can only store one thing over here, just give me one thing, please!" The match operator responds by returning 1, for true, if there was a match; or 0, for false, if there wasn't a match.
美元变量只能存储一件事,所以,当有一个美元变量放在等号的左边,$变量看起来匹配算子,m / / =右边的标志和电话,“嘿,我只能存储一件事,给我一件事,请!”匹配操作符的响应是返回1,如果存在匹配,则为true;如果没有匹配,则为0。
On the other hand, when an @variable
is on the left hand side of the =
sign, the array looks over to the m//
operator and calls out, "Hey, I can store a bunch of things over here, so give me a bunch of stuff, please!" The match operator responds by returning what matched the capture groups in the regex if there was a match; if there wasn't a match, the match operator returns ()
.
另一方面,当@变量在=符号的左边时,数组会向m//运算符查找,并调用:“嘿,我可以在这里存储很多东西,所以请给我一堆东西!”匹配操作符通过返回匹配regex中的捕获组的内容作为响应;如果没有匹配,匹配操作符返回()。
In the first case, the $variable
is said to provide scalar context
for the match operator. In the second case, the @variable
is said to provide list context
for the match operator. Don't let those terms scare you. You know what they mean now.
在第一种情况下,$变量被称为为match操作符提供标量上下文。在第二种情况下,@variable被称为为match操作符提供列表上下文。不要让这些术语吓到你。你知道他们现在的意思。
Next, when you write this:
接下来,当你写下这些:
my ($H,$M,$S) =
You are creating several variables on the left hand side of the =
sign. In unison, they call out to the match operator on the other side of the =
sign, "Hey, there are many of us over here, give us the bunch of stuff, please! That particular my
syntax provides a list context
for the match operator which is on the right hand side of the =
sign:
您正在=符号的左边创建几个变量。他们异口同声地对=号另一边的报务员喊道:“嘿,这儿有很多人,请把这些东西给我们!”这个特殊的my语法为match操作符提供了一个列表上下文,它位于=号的右边:
my ($group1, $group2) = "abc" =~ m/a(.)(.)/;
say $group1; #=> b
say $group2; #=> c
Note that if the delimiters you use for the match operator are m/.../
, then you don't have to write the leading m
, so typically you will see the example above written as:
注意,如果您用于匹配操作符的分隔符是m/…/,这样你就不用写前导m了,所以通常你会看到上面的例子是这样写的:
my ($group1, $group2) = "abc" =~ /a(.)(.)/;
When you use braces like you did: m{...}{...}
, then you have to write the leading m
.
当你使用像你这样的牙套时:……},然后写出前导m。
#3
0
You can use a simpler regex, which is easier to understand, to do what you want:
你可以使用更简单的正则表达式,更容易理解,做你想做的:
\d{2} #\d means a digit, {2} means twice,
#so this matches two consecutive digits
Here's how you can use that regex:
以下是如何使用regex:
#Just blindly use all three of these in every program:
use strict;
use warnings;
use 5.020;
my $date = "10:37:21 2016.01.02";
my ($H,$M,$S) = $date =~ /\d{2}/g; #g => global, Find all matches in the string
say $H; #say() is the same as print() with a newline at the end
say $M;
say $S;
--output:--
10
37
21
The regex starts at the beginning of the string and looks for two consecutive digits and finds 10, so that is a match; then the regex jumps over the :
and finds 37, so that is a match; then the regex jumps over the :
and finds 21, so that is a match; etc., etc.
regex从字符串的开始处开始,查找两个连续的数字,然后找到10,所以这是一个匹配;然后regex跳过:并找到37,因此这是一个匹配;然后regex跳过:并找到21,因此这是一个匹配;等等,等等。
When you assign all the matches to three variables, the first three matches are assigned to the three variables, and the rest of the matches are discarded.
当您将所有匹配分配给三个变量时,前三个匹配被分配给这三个变量,其余的匹配被丢弃。