gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:18:20 24 4
gpt4 key购买 nike

我正在编辑一个 Perl 文件,但我不明白这个正则表达式比较。有人可以向我解释一下吗?

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变量(至少,直到下一个正则表达式出现,如果括号内有一个)。

关于regex - 这个 Perl 正则表达式是什么意思 : m/(. *?) :(. *?)$/g?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3771456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com