[英]Perl regex String part to array
I have a string: 我有一个字符串:
"2012-szept-17 02:55 - someproblem: 192.167.1.1 since - $somevariables[0] $morevariables[-1]"
and I want to get these out of it into an array 我想把它们从数组中取出
$somevariables[0]
$morevariables[-1]
The problem is that these variables can be named anything else and they could be anywhere in the string. 问题在于这些变量可以使用其他任何名称,并且可以在字符串中的任何位置。 The only thing I know about them is that they start with $
and have [sg]
in the end. 我对它们唯一了解的是它们以$
开头,并以[sg]
结尾。
This is the furthest I got with the regexp 这是我用正则表达式得到的最远的结果
my @fuu = $notimsg =~ m/(\$.+\[.+\])/g;
The problem is that the expression is making this into "$somevariables[0] $morevariables[-1]"
问题在于该表达式使它成为"$somevariables[0] $morevariables[-1]"
If they must be valid variable names (identifiers), then try 如果它们必须是有效的变量名(标识符),请尝试
m/(\$\w+\[[^]]+\])/g
As @Borodin points out, if you really want to make sure you match only identifiers (and not something strange like $3abc[12]
), you can use 正如@Borodin指出的那样,如果您确实要确保只匹配标识符(而不是像$3abc[12]
这样的奇怪东西),则可以使用
m/(\$[a-z_]\w*\[[^]]+\])/gi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.