如何将 Perl 正则表达式的捕获存储到单独的变量中?
我有一个正则表达式:
/abc(def)ghi(jkl)mno(pqr)/igs
如何将每个括号的结果捕获到 3 个不同的变量中,每个括号一个?现在我使用一个数组来捕获所有结果,它们是按顺序出现的,但随后我必须解析它们,并且列表可能会很大。
@results = ($string =~ /abc(def)ghi(jkl)mno(pqr)/igs);
I have a regex:
/abc(def)ghi(jkl)mno(pqr)/igs
How would I capture the results of each parentheses into 3 different variables, one for each parentheses? Right now I using one array to capture all the results, they come out sequential but then I have to parse them and the list could be huge.
@results = ($string =~ /abc(def)ghi(jkl)mno(pqr)/igs);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你的问题对我来说有点模棱两可,但我认为你想做这样的事情:
Your question is a bit ambiguous to me, but I think you want to do something like this:
从 5.10 开始,您也可以使用命名捕获缓冲区:
输出:
对于早期版本版本中,您可以使用以下内容,从而避免为每个捕获的缓冲区添加一行:
输出:
但我喜欢将相关数据保留在单个数据结构中,因此最好重新使用哈希。然而,这确实需要一个辅助数组:
或者,如果变量的名称确实是
first
、second
等,或者缓冲区的名称并不重要,但只有订单可以,您可以使用:Starting with 5.10, you can use named capture buffers as well:
Output:
For earlier versions, you can use the following which avoids having to add a line for each captured buffer:
Output:
But I like keeping related data in a single data structure, so it is best to go back to using a hash. This does require an auxiliary array, however:
Or, if the names of the variables really are
first
,second
etc, or if the names of the buffers don't matter but only order does, you can use:另一种方法看起来像 Ghostdog74 的答案,但使用存储哈希引用的数组:
这里的主要优点是使用单个数据结构,并且具有良好的可读循环。
An alternate way of doing it would look like ghostdog74's answer, but using an array that stores hash references:
with the main advantage here of using a single data structure, AND having a nice readable loop.
@OP,当捕获括号时,您可以使用变量 $1,$2....这些是反向引用
输出
@OP, when parenthesis are captured, you can use the variables $1,$2....these are backreferences
output
您可以拥有三个不同的正则表达式,每个正则表达式专注于特定的组。显然,您希望将不同的组分配给正则表达式中的不同数组,但我认为您唯一的选择是将正则表达式分开。
You could have three different regex's each focusing on specific groups. Obviously, you would like to just assign different groups to different arrays in the regex, but I think your only option is to split the regex up.
您可以编写包含命名捕获组的正则表达式。您可以在捕获组开头使用
?
构造来执行此操作:然后您可以使用
$+{myvar}
形式引用那些命名的捕获组。这是一个人为的示例:
给定一个典型的密码文件,它会提取 systemd 用户并返回减去 systemd 前缀的名称。它使用名为
myvar
的捕获组。这只是一个示例,用于说明捕获组变量的使用。You can write a regex containing named capture groups. You do this with the
?<myvar>
construct at the beginning of the capture group:You may then refer to those named capture groups using a
$+{myvar}
form.Here is a contrived example:
Given a typical password file, it pulls out the systemd users and returns the names less the systemd prefix. It uses a capture group named
myvar
. This is just an example thrown together to illustrate the use of capture group variables.