What does the following syntax mean in Perl?
Perl中的以下语法是什么意思?
$line =~ /([^:]+):/;
and
$line =~ s/([^:]+):/$replace/;
7 个解决方案
#1
见perldoc perlreref
[^:]
is a character class that matches any character other than ':'
.
是一个匹配除“:”以外的任何字符的字符类。
[^:]+
means match one or more of such characters.
意味着匹配一个或多个这样的字符。
I am not sure the capturing parentheses are needed. In any case,
我不确定是否需要捕获括号。在任何情况下,
([^:]+):
captures a sequence of one or more non-colon characters followed by a colon.
捕获一个或多个非冒号字符的序列,后跟冒号。
#2
$line =~ /([^:]+):/;
The =~ operator is called the binding operator, it runs a regex or substitution against a scalar value (in this case $line). As for the regex itself, ()
specify a capture. Captures place the text that matches them in special global variables. These variables are numbered starting from one and correspond to the order the parentheses show up in, so given
=〜运算符称为绑定运算符,它运行正则表达式或替换标量值(在本例中为$ line)。至于正则表达式本身,()指定一个捕获。捕获将与它们匹配的文本放在特殊的全局变量中。这些变量从一开始编号,并且对应于括号显示的顺序,因此给出
"abc" =~ /(.)(.)(.)/;
the $1
variable will contain "a"
, the $2
variable will contain "b"
, and the $3
variable will contain "c"
(if you haven't guessed yet .
matches one character*
). []
specifies a character class. Character classes will match one character in them, so /[abc]/
will match one character if it is "a"
, "b"
, or "c"
. Character classes can be negated by starting them with ^
. A negated character class matches one character that is not listed in it, so [^abc]
will match one character that is not "a"
, "b"
, or "c"
(for instance, "d"
will match). The +
is called a quantifier. Quantifiers tell you how many times the preceding pattern must match. +
requires the pattern to match one or more times. (the *
quantifier requires the pattern to match zero or more times). The :
has no special meaning to the regex engine, so it just means a literal :
.
$ 1变量将包含“a”,$ 2变量将包含“b”,而$ 3变量将包含“c”(如果您还没有猜到。匹配一个字符*)。 []指定一个字符类。字符类将匹配其中的一个字符,因此/ [abc] /将匹配一个字符,如果它是“a”,“b”或“c”。使用^启动它们可以取消字符类。否定字符类匹配其中未列出的一个字符,因此[^ abc]将匹配一个不是“a”,“b”或“c”的字符(例如,“d”将匹配)。 +被称为量词。量词会告诉你前面的模式必须匹配多少次。 +要求模式匹配一次或多次。 (*量词需要模式匹配零次或多次)。 :对正则表达式引擎没有特殊意义,所以它只是一个文字:。
So, putting that information together we can see that the regex will match one or more non-colon characters (saving this part to $1
) followed by a colon.
因此,将这些信息放在一起我们可以看到正则表达式将匹配一个或多个非冒号字符(将此部分保存为$ 1),然后是冒号。
$line =~ s/([^:]+):/$replace/;
This is a substitution. Substitutions have two parts, the regex, and the replacement string. The regex part follows all of the same rules as normal regexes. The replacement part is treated like a double quoted string. The substitution replaces whatever matches the regex with the replacement, so given the following code
这是一种替代。替换有两部分,正则表达式和替换字符串。正则表达式部分遵循与普通正则表达式相同的所有规则。替换部件被视为双引号字符串。替换替换正则表达式与替换的匹配,因此给出以下代码
my $line = "key: value";
my $replace = "option";
$line =~ s/([^:]+):/$replace/;
The $line variable will hold the string "option value"
.
$ line变量将保存字符串“option value”。
You may find it useful to read perldoc perlretut
.
您可能会发现阅读perldoc perlretut非常有用。
*
except newline, unless the /m option is used, in which case it matches any character
*除了换行符,除非使用/ m选项,在这种情况下它匹配任何字符
#3
The first one captures the part in front of a colon from a line, such as "abc" in the string "abc:foo". More precisely it matches at least one non-colon character (though as many as possible) directly before a colon and puts them into a capture group.
第一个从一行中捕获冒号前面的部分,例如字符串“abc:foo”中的“abc”。更确切地说,它在冒号之前直接匹配至少一个非冒号字符(尽可能多)(并尽可能多地将它们放入捕获组)。
The second one substitutes said part, although this time including the colon by the contents of the variable $replace
.
第二个替换所述部分,虽然这次包括冒号变量$ replace的内容。
#4
I may be misunderstanding some of the previous answers, but I think that there's a confusion about the second example. It will not replace only the captured item (i.e., one or more non-colons up until a colon) by $replaced. It will replace all of ([^:]+):
with $replace
- the colon as well. (The substitution operates on the match, not just the capture.)
我可能误解了之前的一些答案,但我认为第二个例子存在混淆。它不会仅取代被取代的捕获物品(即直到冒号的一个或多个非冒号)。它将替换所有([^:] +):$ replace - 冒号。 (替换操作在匹配上,而不仅仅是捕获。)
This means if you don't include a colon in $replace
(and you want one), you will get bit:
这意味着如果你在$ replace中没有包含冒号(你想要一个冒号),你会得到一点:
my $line = 'http://www.example.com/';
my $replace = 'ftp';
$line =~ s/([^:]+):/$replace/;
print "Here's \$line now: $line\n";
Output:
Here's $line now: ftp//www.example.com/ # Damn, no colon!
I'm not sure if you are just looking at example code, but you unless you plan to use the capture I'm not sure you really want it in these examples.
我不确定你是否只是在查看示例代码,但除非你打算使用捕获,否则你不确定在这些示例中是否真的需要它。
If you are very unfamiliar with regular expressions (or Perl), you should look at perldoc perlrequick
before trying perldoc perlre
or perldoc perlretut
.
如果您对正则表达式(或Perl)非常不熟悉,那么在尝试perldoc perlre或perldoc perlretut之前,您应该先查看perldoc perlrequick。
#5
You want to return something matching one or more characters that are anything but : followed by a : and the second one you want to do the same thing but replace it with $replace.
你想要返回匹配一个或多个字符的东西,但是:后跟一个:和第二个你要做同样的事情,但用$ replace替换它。
#6
perl -MYAPE::Regex::Explain -e "print YAPE::Regex::Explain->new('([^:]+):')->explain"
perl -MYAPE :: Regex :: Explain -e“print YAPE :: Regex :: Explain-> new('([^:] +):') - >解释”
The regular expression:
(?-imsx:([^:]+):)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[^:]+ any character except: ':' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
: ':'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
#7
$line =~ /([^:]+):/;
Matches anything that does not contain : before :/
匹配任何不包含的内容:之前:/
If $line = "http://www.google.com", it will match http (the variable $1 will contain http)
如果$ line =“http://www.google.com”,它将匹配http(变量$ 1将包含http)
$line =~ s/([^:]+):/$replace/;
This time, replace the value matched by the content of the variable $replace
这次,替换变量$ replace的内容匹配的值
#1
见perldoc perlreref
[^:]
is a character class that matches any character other than ':'
.
是一个匹配除“:”以外的任何字符的字符类。
[^:]+
means match one or more of such characters.
意味着匹配一个或多个这样的字符。
I am not sure the capturing parentheses are needed. In any case,
我不确定是否需要捕获括号。在任何情况下,
([^:]+):
captures a sequence of one or more non-colon characters followed by a colon.
捕获一个或多个非冒号字符的序列,后跟冒号。
#2
$line =~ /([^:]+):/;
The =~ operator is called the binding operator, it runs a regex or substitution against a scalar value (in this case $line). As for the regex itself, ()
specify a capture. Captures place the text that matches them in special global variables. These variables are numbered starting from one and correspond to the order the parentheses show up in, so given
=〜运算符称为绑定运算符,它运行正则表达式或替换标量值(在本例中为$ line)。至于正则表达式本身,()指定一个捕获。捕获将与它们匹配的文本放在特殊的全局变量中。这些变量从一开始编号,并且对应于括号显示的顺序,因此给出
"abc" =~ /(.)(.)(.)/;
the $1
variable will contain "a"
, the $2
variable will contain "b"
, and the $3
variable will contain "c"
(if you haven't guessed yet .
matches one character*
). []
specifies a character class. Character classes will match one character in them, so /[abc]/
will match one character if it is "a"
, "b"
, or "c"
. Character classes can be negated by starting them with ^
. A negated character class matches one character that is not listed in it, so [^abc]
will match one character that is not "a"
, "b"
, or "c"
(for instance, "d"
will match). The +
is called a quantifier. Quantifiers tell you how many times the preceding pattern must match. +
requires the pattern to match one or more times. (the *
quantifier requires the pattern to match zero or more times). The :
has no special meaning to the regex engine, so it just means a literal :
.
$ 1变量将包含“a”,$ 2变量将包含“b”,而$ 3变量将包含“c”(如果您还没有猜到。匹配一个字符*)。 []指定一个字符类。字符类将匹配其中的一个字符,因此/ [abc] /将匹配一个字符,如果它是“a”,“b”或“c”。使用^启动它们可以取消字符类。否定字符类匹配其中未列出的一个字符,因此[^ abc]将匹配一个不是“a”,“b”或“c”的字符(例如,“d”将匹配)。 +被称为量词。量词会告诉你前面的模式必须匹配多少次。 +要求模式匹配一次或多次。 (*量词需要模式匹配零次或多次)。 :对正则表达式引擎没有特殊意义,所以它只是一个文字:。
So, putting that information together we can see that the regex will match one or more non-colon characters (saving this part to $1
) followed by a colon.
因此,将这些信息放在一起我们可以看到正则表达式将匹配一个或多个非冒号字符(将此部分保存为$ 1),然后是冒号。
$line =~ s/([^:]+):/$replace/;
This is a substitution. Substitutions have two parts, the regex, and the replacement string. The regex part follows all of the same rules as normal regexes. The replacement part is treated like a double quoted string. The substitution replaces whatever matches the regex with the replacement, so given the following code
这是一种替代。替换有两部分,正则表达式和替换字符串。正则表达式部分遵循与普通正则表达式相同的所有规则。替换部件被视为双引号字符串。替换替换正则表达式与替换的匹配,因此给出以下代码
my $line = "key: value";
my $replace = "option";
$line =~ s/([^:]+):/$replace/;
The $line variable will hold the string "option value"
.
$ line变量将保存字符串“option value”。
You may find it useful to read perldoc perlretut
.
您可能会发现阅读perldoc perlretut非常有用。
*
except newline, unless the /m option is used, in which case it matches any character
*除了换行符,除非使用/ m选项,在这种情况下它匹配任何字符
#3
The first one captures the part in front of a colon from a line, such as "abc" in the string "abc:foo". More precisely it matches at least one non-colon character (though as many as possible) directly before a colon and puts them into a capture group.
第一个从一行中捕获冒号前面的部分,例如字符串“abc:foo”中的“abc”。更确切地说,它在冒号之前直接匹配至少一个非冒号字符(尽可能多)(并尽可能多地将它们放入捕获组)。
The second one substitutes said part, although this time including the colon by the contents of the variable $replace
.
第二个替换所述部分,虽然这次包括冒号变量$ replace的内容。
#4
I may be misunderstanding some of the previous answers, but I think that there's a confusion about the second example. It will not replace only the captured item (i.e., one or more non-colons up until a colon) by $replaced. It will replace all of ([^:]+):
with $replace
- the colon as well. (The substitution operates on the match, not just the capture.)
我可能误解了之前的一些答案,但我认为第二个例子存在混淆。它不会仅取代被取代的捕获物品(即直到冒号的一个或多个非冒号)。它将替换所有([^:] +):$ replace - 冒号。 (替换操作在匹配上,而不仅仅是捕获。)
This means if you don't include a colon in $replace
(and you want one), you will get bit:
这意味着如果你在$ replace中没有包含冒号(你想要一个冒号),你会得到一点:
my $line = 'http://www.example.com/';
my $replace = 'ftp';
$line =~ s/([^:]+):/$replace/;
print "Here's \$line now: $line\n";
Output:
Here's $line now: ftp//www.example.com/ # Damn, no colon!
I'm not sure if you are just looking at example code, but you unless you plan to use the capture I'm not sure you really want it in these examples.
我不确定你是否只是在查看示例代码,但除非你打算使用捕获,否则你不确定在这些示例中是否真的需要它。
If you are very unfamiliar with regular expressions (or Perl), you should look at perldoc perlrequick
before trying perldoc perlre
or perldoc perlretut
.
如果您对正则表达式(或Perl)非常不熟悉,那么在尝试perldoc perlre或perldoc perlretut之前,您应该先查看perldoc perlrequick。
#5
You want to return something matching one or more characters that are anything but : followed by a : and the second one you want to do the same thing but replace it with $replace.
你想要返回匹配一个或多个字符的东西,但是:后跟一个:和第二个你要做同样的事情,但用$ replace替换它。
#6
perl -MYAPE::Regex::Explain -e "print YAPE::Regex::Explain->new('([^:]+):')->explain"
perl -MYAPE :: Regex :: Explain -e“print YAPE :: Regex :: Explain-> new('([^:] +):') - >解释”
The regular expression:
(?-imsx:([^:]+):)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[^:]+ any character except: ':' (1 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
: ':'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
#7
$line =~ /([^:]+):/;
Matches anything that does not contain : before :/
匹配任何不包含的内容:之前:/
If $line = "http://www.google.com", it will match http (the variable $1 will contain http)
如果$ line =“http://www.google.com”,它将匹配http(变量$ 1将包含http)
$line =~ s/([^:]+):/$replace/;
This time, replace the value matched by the content of the variable $replace
这次,替换变量$ replace的内容匹配的值