in reply to Quick regex

Like this?

$s = 'fred_bill_john_jack_joe_mary_eugene';; $s =~ /(^[^_]+_[^_]+_[^_]+_)/ and print "'$1'";; 'fred_bill_john_'

Or this?

$s =~ /(^(?:[^_]+_){3})/ and print "'$1'";; 'fred_bill_john_'

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP PCW It is as I've been saying!(Audio until 20090817)

Replies are listed 'Best First'.
Re^2: Quick regex
by Karger78 (Beadle) on Nov 24, 2009 at 17:34 UTC
    that looks good, but how do i kill the trailing _ So your example would look like this 'fred_bill_john'

      You want to match

      name, dash, name, dash, name

      or

      ( name, dash ) x 2, name

      so

      my ($substr) = $s =~ /^((?:[^_]*_){2}[^_]*)/ or die("No match\n");
      Everything in ( ) goes into $1, so move the last _ outside of )