1

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

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

This allows variable names starting with a digit. The correct regex is /(\$[a-z_]\w*\[[^]]+\])/i
I figure the array at the end makes it rather unlikely to crop up...after all, his problem is avoiding the spaces. Nevertheless, I'll edit to add.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.