Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 这个Perl正则表达式是什么意思:m/(.*):(.*)$/g?_Regex_Perl - Fatal编程技术网

Regex 这个Perl正则表达式是什么意思:m/(.*):(.*)$/g?

Regex 这个Perl正则表达式是什么意思:m/(.*):(.*)$/g?,regex,perl,Regex,Perl,我正在编辑一个Perl文件,但我不理解这个regexp比较。谁能给我解释一下吗 if ($lines =~ m/(.*?):(.*?)$/g) { } .. 这里发生了什么$lines是文本文件中的一行。(.*)捕获任何字符,但尽可能少 因此,它会查找类似于:的模式,如果字符串中有多个:,则第一个模式将用作和之间的分隔符,将其拆分为多个部分: $lines =~ m/ (.*?) # Match any character (except newlines)

我正在编辑一个Perl文件,但我不理解这个regexp比较。谁能给我解释一下吗

if ($lines =~ m/(.*?):(.*?)$/g) { } .. 
这里发生了什么
$lines
是文本文件中的一行。

(.*)
捕获任何字符,但尽可能少


因此,它会查找类似于
的模式,如果字符串中有多个
,则第一个模式将用作

之间的分隔符,将其拆分为多个部分:

$lines =~ m/ (.*?)      # Match any character (except newlines)
                        # zero or more times, not greedily, and
                        # stick the results in $1.
             :          # Match a colon.
             (.*?)      # Match any character (except newlines)
                        # zero or more times, not greedily, and
                        # stick the results in $2.
             $          # Match the end of the line.
           /gx;
因此,这将匹配字符串,如:“”(它匹配零个字符,然后是冒号,然后是行尾前的零个字符,
$1
$2
是空字符串),或
“abc:
$1=“abc”
$2
是空字符串),或
“abc:def:ghi”
$1=“abc”
$2=“def:ghi”


如果您传入一行不匹配的代码(如果字符串不包含冒号,则看起来是这样),那么它将不会处理括号内的代码。但是如果它确实匹配,那么括号内的代码可以使用和处理特殊的
$1
$2
变量(至少,直到下一个正则表达式出现,如果括号内有一个正则表达式)。

那一行表示在
$line
上使用regex
m/(*?)(*?)执行正则表达式匹配$/g
。如果在
$行
中找到匹配项,它将有效地返回
true
,如果找不到匹配项,它将有效地返回
false

=~
运算符的说明:

二进制“=~”绑定标量表达式 进行模式匹配。某些操作 搜索或修改字符串$\uBy 违约这个接线员就是这样做的 在其他方面的操作工作 一串正确的论点是搜索 模式、替代或 音译。左派论点是 应该搜查的东西, 替换的,或音译的 默认$\的。在标量中使用时 上下文,返回值通常为 表示该项目的成功 手术

正则表达式本身是:

m/    #Perform a "match" operation
(.*?) #Match zero or more repetitions of any characters, but match as few as possible (ungreedy)
:     #Match a literal colon character
(.*?) #Match zero or more repetitions of any characters, but match as few as possible (ungreedy)
$     #Match the end of string
/g    #Perform the regex globally (find all occurrences in $line)

因此,如果
$lines
与该正则表达式匹配,它将进入条件部分,否则它将为
false
,并将跳过它。

有一个工具可以帮助理解正则表达式:

忽略
g
修饰符,此处不需要该修饰符:

use strict;
use warnings;
use YAPE::Regex::Explain;

my $re = qr/(.*?):(.*?)$/;
print YAPE::Regex::Explain->new($re)->explain();

__END__

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 \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  :                        ':'
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

另请参见。

它是由对正则表达式了解太多或对和变量了解不够的人编写的

这本可以写成

if ($lines =~ /:/) {
    ... # use $` ($PREMATCH)  instead of $1
    ... # use $' ($POSTMATCH) instead of $2
}


看起来第一个
(.*)
将始终与空字符串匹配。不总是。它将匹配第一个冒号之前的所有字符。如果要使用/:/,请使用/p标志以及Perl 5.10中的${^PREMATCH}和${^POSTMATCH}变量。不过,我更喜欢分裂,因为这才是真正发生的事情。
if ( ($var1,$var2) = split /:/, $lines, 2 and defined($var2) ) {
    ... # use $var1, $var2 instead of $1,$2
}