TMTOWTDI

Discussion in 'Perl Misc' started by Graham Gough, Feb 19, 2004.

  1. Graham Gough

    Graham Gough Guest

    I have been trying to solve a problem in which I wish to substitute
    for *almost* all occurrences of a regular expression. For example all
    occurrences of \d\d\d\d except some specific ones. I ended up with
    using the e modifier and code like that given below, but had a nagging
    felling that there was probably some more elegant method. Anyone any
    suggestions?

    Graham

    $text =~ s/(\d\d\d\d)/&choose($1,"1234")/ge;

    sub choose {
    my $val = shift;
    my $notpat = shift;
    my $ret=$val;
    if (!($val =~ /$notpat/)) {
    $ret = "XX" . $ret . "XX";
    }
    return $ret;
    }
     
    Graham Gough, Feb 19, 2004
    #1
    1. Advertising

  2. Graham Gough

    fifo Guest

    At 2004-02-19 17:02 +0000, Graham Gough wrote:
    > I have been trying to solve a problem in which I wish to substitute
    > for *almost* all occurrences of a regular expression. For example all
    > occurrences of \d\d\d\d except some specific ones. I ended up with
    > using the e modifier and code like that given below, but had a nagging
    > felling that there was probably some more elegant method. Anyone any
    > suggestions?
    >
    > Graham
    >
    > $text =~ s/(\d\d\d\d)/&choose($1,"1234")/ge;
    >
    > sub choose {
    > my $val = shift;
    > my $notpat = shift;
    > my $ret=$val;
    > if (!($val =~ /$notpat/)) {
    > $ret = "XX" . $ret . "XX";
    > }
    > return $ret;
    > }
    >


    How about

    $text =~ s/(?!1234)(\d{4})/XX$1XX/g;
     
    fifo, Feb 19, 2004
    #2
    1. Advertising

  3. fifo <> wrote:
    > At 2004-02-19 17:02 +0000, Graham Gough wrote:
    > > I have been trying to solve a problem in which I wish to substitute
    > > for *almost* all occurrences of a regular expression. For example all
    > > occurrences of \d\d\d\d except some specific ones. I ended up with

    [...]
    >
    > How about
    >
    > $text =~ s/(?!1234)(\d{4})/XX$1XX/g;


    Depending on the OP's data, may have undesired matches, for example, if
    $text="12345"

    Perhaps, if the OP only cares about 4 digit numbers, and suppose he
    doesnt want to match 1234 or 6789 or 4242:
    $text =~ s/(^|\D)(?!1234|6789|4242)(\d{4})(\D|$)/$1<<$2>>$3/g;

    --
    Glenn Jackman
    NCF Sysadmin
     
    Glenn Jackman, Feb 19, 2004
    #3
  4. Graham Gough

    Anno Siegel Guest

    Graham Gough <> wrote in comp.lang.perl.misc:
    > I have been trying to solve a problem in which I wish to substitute
    > for *almost* all occurrences of a regular expression. For example all
    > occurrences of \d\d\d\d except some specific ones. I ended up with
    > using the e modifier and code like that given below, but had a nagging
    > felling that there was probably some more elegant method. Anyone any
    > suggestions?


    Unless the exceptions can be reasonably described through a regex,
    the /e modifier is bound to crop up. Nothing wrong with that.

    > $text =~ s/(\d\d\d\d)/&choose($1,"1234")/ge;
    >
    > sub choose {
    > my $val = shift;
    > my $notpat = shift;
    > my $ret=$val;
    > if (!($val =~ /$notpat/)) {
    > $ret = "XX" . $ret . "XX";
    > }
    > return $ret;
    > }


    There are a few keywords that make a Perl programmer think "Hash!", and
    one of them is "a few exceptions". (Another such keyword is "unique").
    In that vein, I'd go for (leaving out the obvious setup of %exceptions,
    and untested)

    s/(\d{4})/exists $exceptions{ $1} ? $1 : "XX$1XX"/ge;

    Anno
     
    Anno Siegel, Feb 19, 2004
    #4
  5. Graham Gough

    Uri Guttman Guest

    >>>>> "GJ" == Glenn Jackman <> writes:

    >> How about
    >>
    >> $text =~ s/(?!1234)(\d{4})/XX$1XX/g;


    GJ> Depending on the OP's data, may have undesired matches, for example, if
    GJ> $text="12345"

    GJ> Perhaps, if the OP only cares about 4 digit numbers, and suppose he
    GJ> doesnt want to match 1234 or 6789 or 4242:
    GJ> $text =~ s/(^|\D)(?!1234|6789|4242)(\d{4})(\D|$)/$1<<$2>>$3/g;

    i would still go with /e but just be simpler than the OP's code.

    $text =~ s/(\d{4})/ $1 eq '1234' ? $1 : "XX$1XX" /eg;

    if you need to exclude many sets of 4 digits, put them in a hash:

    my %keepers = map { $_ => 1 } qw( 1234 5678 9012 ) ;

    $text =~ s/(\d{4})/ $keepers{$1} ? $1 : "XX$1XX" /eg;

    that is clear, fast and modifiable by others.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Feb 20, 2004
    #5
  6. Anno Siegel <-berlin.de> wrote:

    > There are a few keywords that make a Perl programmer think "Hash!", and
    > one of them is "a few exceptions". (Another such keyword is "unique").



    Yet some more:

    lookup
    cross-reference
    related to ( a "relation" )
    associated with
    duplicate
    symbolic reference
    dispatch table


    Sometimes you can cut right through that thinking stuff and
    head right on over to the solution:

    Message-ID: <>

    I don't know what your original problem is,
    but I suggest to use a hash. --Rafael Garcia-Suarez


    :)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 20, 2004
    #6
  7. Graham Gough

    Uri Guttman Guest

    >>>>> "TM" == Tad McClellan <> writes:

    TM> Anno Siegel <-berlin.de> wrote:
    >> There are a few keywords that make a Perl programmer think "Hash!", and
    >> one of them is "a few exceptions". (Another such keyword is "unique").



    TM> Yet some more:

    TM> lookup
    TM> cross-reference
    TM> related to ( a "relation" )
    TM> associated with
    TM> duplicate
    TM> symbolic reference
    TM> dispatch table

    table (of any sort)
    record
    structure
    convert
    is a
    set
    has a
    tree
    mapping (or map)
    dictionary
    objects (most use hashes)
    attributes
    properties
    traits
    registry

    this is actually an interesting list and maybe could be converted into
    an FAQ: when should i use a hash? :)

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Feb 20, 2004
    #7
  8. Graham Gough

    Graham Gough Guest

    Thanks for all the interesting suggestions. Like some of the posters,
    my usual reaction to many programming problems is to use a hash, I
    don't know why I didn't think of it this time. Old age, probably

    Thanks again

    Graham
     
    Graham Gough, Feb 20, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. h3xx
    Replies:
    5
    Views:
    90
    Peter Wyzl
    Feb 22, 2009
Loading...

Share This Page