Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

regexp Non-capturing Grouping failure

by fireartist (Chaplain)
on Sep 01, 2004 at 09:51 UTC ( [id://387459]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://387459]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-26 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found