简体   繁体   中英

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.

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]"

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

m/(\$[a-z_]\w*\[[^]]+\])/gi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM