Replace exactly match of a number

Discussion in 'Perl Misc' started by Tambaa Hapa, Jul 13, 2005.

  1. Tambaa Hapa

    Tambaa Hapa Guest

    I am trying to replace a number with some string. For example, replace &1
    with hello. However using s/&$num/hello/g where $num = 1 also replaces &10
    to hello0. Anyone have any ideas on how to do this?

    Thanks in advance
    TH
    Tambaa Hapa, Jul 13, 2005
    #1
    1. Advertising

  2. "Tambaa Hapa" <> wrote in
    news:db20oa$68$:

    > I am trying to replace a number with some string. For example, replace
    > &1 with hello. However using s/&$num/hello/g where $num = 1 also
    > replaces &10 to hello0. Anyone have any ideas on how to do this?


    Isn't it a little too tedious to write your own templating system?

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Jul 13, 2005
    #2
    1. Advertising

  3. Tambaa Hapa

    Dave Weaver Guest

    Tambaa Hapa <> wrote:
    > I am trying to replace a number with some string. For example, replace &1
    > with hello. However using s/&$num/hello/g where $num = 1 also replaces &10
    > to hello0. Anyone have any ideas on how to do this?


    s/&$num(?!\d)/hello/g;

    see "perldoc perlre" for details.
    Dave Weaver, Jul 13, 2005
    #3
  4. Tambaa Hapa

    Paul Lalli Guest

    Tambaa Hapa wrote:
    > I am trying to replace a number with some string. For example, replace &1
    > with hello. However using s/&$num/hello/g where $num = 1 also replaces &10
    > to hello0. Anyone have any ideas on how to do this?


    How about making sure the next thing is not a digit?

    s/&$num(?!\d)/hello/g;

    Read more about look-ahead assertions in:
    perldoc perlre
    perldoc perlretut
    and probably one or two others I'm forgetting.

    Paul Lalli
    Paul Lalli, Jul 13, 2005
    #4
  5. Tambaa Hapa

    Tambaa Hapa Guest

    Thanks a bunch Dave. That certainly did the trick. I guess I just needed a
    little imagination!

    TH

    "Dave Weaver" <> wrote in message
    news:42d4e0c8$0$24082$...
    > Tambaa Hapa <> wrote:
    > > I am trying to replace a number with some string. For example, replace

    &1
    > > with hello. However using s/&$num/hello/g where $num = 1 also replaces

    &10
    > > to hello0. Anyone have any ideas on how to do this?

    >
    > s/&$num(?!\d)/hello/g;
    >
    > see "perldoc perlre" for details.
    >
    >
    Tambaa Hapa, Jul 13, 2005
    #5
  6. Tambaa Hapa

    Tambaa Hapa Guest

    Thanks Paul. I could not figure out how to do the look ahead. I tried
    something like s/&$num[^\d]/hello/g but that replaced the next character
    too, which is not what I wanted. Your suggestion (and Dave's) did the trick.
    Thanks again.

    TH

    "Paul Lalli" <> wrote in message
    news:...
    > Tambaa Hapa wrote:
    > > I am trying to replace a number with some string. For example, replace

    &1
    > > with hello. However using s/&$num/hello/g where $num = 1 also replaces

    &10
    > > to hello0. Anyone have any ideas on how to do this?

    >
    > How about making sure the next thing is not a digit?
    >
    > s/&$num(?!\d)/hello/g;
    >
    > Read more about look-ahead assertions in:
    > perldoc perlre
    > perldoc perlretut
    > and probably one or two others I'm forgetting.
    >
    > Paul Lalli
    >
    Tambaa Hapa, Jul 13, 2005
    #6
  7. Tambaa Hapa

    Eric Bohlman Guest

    "Tambaa Hapa" <> wrote in
    news:db3109$jho$:

    > Thanks Paul. I could not figure out how to do the look ahead. I tried
    > something like s/&$num[^\d]/hello/g but that replaced the next
    > character too, which is not what I wanted. Your suggestion (and
    > Dave's) did the trick. Thanks again.


    I would, though, seriously take up Sinan's suggestion and go over to
    http://search.cpan.org and search for "template"; there are tons of
    excellent template-substitution modules and you might save yourself hours
    of wheel-reinvention.
    Eric Bohlman, Jul 14, 2005
    #7
    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. ccm news
    Replies:
    0
    Views:
    3,051
    ccm news
    Jan 15, 2009
  2. John Gordon
    Replies:
    13
    Views:
    453
    Ian Kelly
    Dec 20, 2011
  3. Merlin
    Replies:
    3
    Views:
    117
    Jeff 'japhy' Pinyan
    Jan 31, 2004
  4. Gancy
    Replies:
    4
    Views:
    162
    Rasto Levrinc
    Feb 3, 2005
  5. Replies:
    3
    Views:
    163
    Brian McCauley
    Sep 12, 2005
Loading...

Share This Page