in reply to Some more regex
That just skips over the [, ], and spaces around your = pairs.$x="[x=1 y=3 z=5]"; print join ":", $x=~/(\w+)=(\d+)/g;
The above will treat foo-bar=5 as bar=5, ignoring the "foo-" since it has a non-\w char. To know that your regex is parsing the complete string successfully, do it like this:
This is a very useful idiom (to me anyway). It looks fairly complicated at first, but once you go through and understand it, you can see that most of it stays the same for different applications. You just replace the first \[, the data line, the \s, and the last \[ as appropriate for what you are parsing.print join ":", $x=~/\G # start where prev match left off (?:\A\[)? # if start of string, expect [ (\w+)=(\d+) # our data (?:\s(?!\z) # between pairs, expect \s |\]\z) # or if end of string, expect ] /gcx; # if whole string was parsed, pos will be defined and equal to length print "error" unless pos($x) && pos($x) == length($x);
|
|---|