in reply to regexp help -- grab almost a whole string

What I need is a single regexp that will get the result the first time. It is to be returned from a subroutine kinda like this:
sub get_regexp { if ($blah) $regexp = qr {}; elsif ($blahblah) $regexp = qr {}; }

calling program

$REGEXP = get_regexp($ip); ($circuit_id) = $if_descr =~ $REGEXP;

I want to get the correct result with one regexp without having to do any cleanup after the fact (ie removing the dot in a different step).

There must be a way to do this? If not, I will simply leave the dot in.

TDR

Replies are listed 'Best First'.
Re^2: regexp help -- grab almost a whole string
by kennethk (Abbot) on May 19, 2009 at 19:58 UTC
    Since your strings are separated, they must be captured in separate buffers and there is no way to join them with just a matching regular expression. If you change the calling program to implement the join solution I suggested above, it will not break existing behavior, except that failed matches will store a null string ("") in $circuit_id in place of an undef (both of which still evaluate false). If you cannot change the calling program, then I see no solution.

    $REGEXP = get_regexp($ip); $circuit_id = join q{}, $if_descr =~ $REGEXP;
Re^2: regexp help -- grab almost a whole string
by grizzley (Chaplain) on May 20, 2009 at 05:57 UTC

    I think I did similar mistake while designing new test system at work. The assumption was, that every value can be tested and checked with a regexp.

    $regexp = ...; if($value !~ /$regexp/) { $err .= "wrong value ($value)" }

    Everything was fine. And then came the issue with integer range, i.e. value must be integer between 0 and 12800... Now I think about redesigning the engine to accept sub reference instead of regexp:

    $subref = ...; if(! &$subref($value)) { $err .= "wrong value ($value)" }

    because the only reasonable way of fixing it is to abandon assumption about "regexp can match even the universe" (pity, I like regexps...)

Re^2: regexp help -- grab almost a whole string
by Polyglot (Chaplain) on May 20, 2009 at 03:27 UTC
    I'm not certain why it must be a "single regexp", but this will do the job in a single line of execution:

    if ($if_desc =~ /(DHEC)\.?(\w+)/) {$circuit_id = "$1$2"};

    Would this satisfy your requirement?

    Blessings,

    ~Polyglot~