fireartist has asked for the wisdom of the Perl Monks concerning the following question:

I'm using a non-capturing grouping which seems to be capturing.
I expect the code (?:|([^]]+))? to not capture the pipe character, but it does.
Code:
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; use URI; my $text = <<END; this is some plain text here is a link: [link://forum] and here is another: [mentors://pil_description|Partnerships in Learning] END $text =~ s! \[ (\w+) :// ([\w-]+) (?:|([^]]+))? \] !print("'$1' '$2' '$3'\n")!gsex;
Output:
'link' 'forum' '' 'mentors' 'pil_description' '|Partnerships in Learning' Use of uninitialized value in concatenation (.) or string at test.pl l +ine 13.
Are any of my modifiers (/gsex) affecting the capturing, or is there anything else I'm missing?

Replies are listed 'Best First'.
Re: regexp Non-capturing Grouping failure
by fireartist (Chaplain) on Sep 01, 2004 at 10:02 UTC
    My apologies, I had spent some time trying to figure this out, and then within 5 minutes of posting I worked out that the pipe character needs escaping.
    That snippet should now be (?:\|([^]]+))?
      Howdy!

      ...nothing like explaining it to someone else/blank wall/whatever to bring that flat-forehead moment!

      Been there. Done that.

      yours,
      Michael
      Yes, because the pipe character stands for alternatives.