gpt4 book ai didi

regex - 这个 perl 正则表达式的含义是什么?

转载 作者:行者123 更新时间:2023-12-01 07:39:37 25 4
gpt4 key购买 nike

the regex expression is as below:
if ($ftxt =~ m|/([^=]+)="(.+)"|o)
{
.....
}

这个正则表达式似乎与许多其他正则表达式不同。让我感到困惑的是“|” , 大多数正则表达式使用“/”而不是“|”。而且,组 ([^=]+) 也让我感到困惑。我知道 [^=] 的意思是“字符串的开头”或“=”,但是重复 '^' 一次或多次是什么意思? ,这怎么解释?

最佳答案

您可以使用不同的分隔符代替 / .例如,您可以使用:

m#/([^=]+)="(.+)"#o
或者
m~/([^=]+)="(.+)"~o
这里使用与 / 不同的东西的优势是你不必逃避斜线,否则,你必须使用:
m/\/([^=]+)="(.+)"/o
^
[或 [/] ] ([^=]+)是一个捕获组,在里面,你有 [^=]+ . [^=]是一个否定类,将匹配任何不是 = 的字符. ^在字符类的开头表现不同,与 ^ 不同在字符类之外,这意味着“行的开头”。
最后一部分 o ,这是一个我到目前为止还没有遇到过的标志,所以稍微搜索一下就把我带到了 this post , 我引用:

The /o modifier is in the perlop documentation instead of the perlre documentation since it is a quote-like modifier rather than a regex modifier. That has always seemed odd to me, but that's how it is.

Before Perl 5.6, Perl would recompile the regex even if the variable had not changed. You don't need to do that anymore. You could use /o to compile the regex once despite further changes to the variable, but as the other answers noted, qr// is better for that.

关于regex - 这个 perl 正则表达式的含义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18614109/

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