in reply to Special Variables in Regular Expression

toolic was right, the problem is that extra question mark:
my ($data_in, $data_out); $data_in = q|<container><a>smile!!! </a><b> smile again!! </b></contai +ner><?query?>|; while ($data_in =~ /<container><a>(.+?)<\/a><b>(.+?)<\/b><\/container> +(<\?query\?>)?/ig) { $data_out = "$2, $1$3 "; #errors here ; #further processing here print $data_out; }

If you put back the second question mark at the end you will get the error:

Use of uninitialized value $3 in concatenation (.) or string at perlmo +nks_700143.pl line 8.
If you are new to using regular expressions and are on Windows, you can try this free tool: Regex Coach. It will help you see, graphically, how the regex breaks down and will allow you to step through your expression.

Celebrate Intellectual Diversity

Replies are listed 'Best First'.
Re^2: Special Variables in Regular Expression
by toolic (Bishop) on Jul 25, 2008 at 16:25 UTC
    To decipher foreign regexes, I also use YAPE::Regex::Explain (which is also free because it is on CPAN :). Using the OP's original regex (excluding the modifiers, /ig):
    use warnings; use strict; use YAPE::Regex::Explain; my $re = '<container><a>(.+?)<\/a><b>(.+?)<\/b><\/container>(<\?query\ +?>)??'; print YAPE::Regex::Explain->new($re)->explain;

    produces this output:

    While helpful, I still did not grok the ?? usage :(

Re^2: Special Variables in Regular Expression
by fiat (Initiate) on Jul 28, 2008 at 07:10 UTC

    Thanks for the info InfiniteSilence. RegexCoach is neat.