in reply to Removing only the first instance with RegEx

#!/usr/bin/perl use strict; use warnings; my $bal; $bal = qr /[^()]* (?: [(] (??{ $bal }) [)] )* [^()]*/x; while (<DATA>) { chomp; print $& while /$bal/g; print "\n"; } __DATA__ (1.3.56.84 56.38.m.26) (56.2.3.59) ((one)(two)) ((one(two)) ((one)(two) (one)(two)) ((one)two))

Running this gives:

1.3.56.84 56.38.m.26 (56.2.3.59) ((one)(two)) (one(two)) (one)(two) (one)(two) ((one)two)

Abigail

Replies are listed 'Best First'.
Re: Re: Removing only the first instance with RegEx
by Anonymous Monk on Nov 06, 2003 at 14:22 UTC
    For the most part I understand what it's doing, but could you explain the '*' in the RegEx?

    Thanks

      For the most part I understand what it's doing, but could you explain the '*' in the RegEx?

      Fascinating. You understand compiled regular expressions, delayed regular expressions, $&, but not '*', one of the most used special tokens in a regular expression?

      Anyway, as 'man perlre' could have told you, '*' means that the thing in front of it can be matched zero or more times.

      Abigail

        Wow...it's too early in the AM. I did know what'*' (zero or more) and '+' (one or more) mean.

        sorry!