How replace ing2004 to ing-2004 ? please help

Discussion in 'Perl Misc' started by François, Oct 6, 2003.

  1. François

    François Guest

    I want to replace an expression abc1234 to abc-1234 how can i do
    I 've tried

    s/......./$1$2$3-$4$5$6$7/ but it dont work.

    Please help ?

    Thanks a lot
     
    François, Oct 6, 2003
    #1
    1. Advertising

  2. In article <blrqe4$i6a$>, François wrote:
    > I want to replace an expression abc1234 to abc-1234 how can i do
    > I 've tried
    >
    > s/......./$1$2$3-$4$5$6$7/ but it dont work.



    s/(\w{3})(\d{4})/$1-$2/


    --
    Andreas Kähäri
     
    Andreas Kahari, Oct 6, 2003
    #2
    1. Advertising

  3. Bernard El-Hagin wrote:
    >
    > "François" <> wrote in news:blrqe4$i6a$1
    > @s1.read.news.oleane.net:
    >
    > > I want to replace an expression abc1234 to abc-1234 how can i do
    > > I 've tried
    > >
    > > s/......./$1$2$3-$4$5$6$7/ but it dont work.
    > >
    > > Please help ?

    >
    > s/(\D+)(\d+)/$1-$2/


    TMTOWTDI

    I'd use
    s/(\D{3})(\d{4})/$1-$2/

    It's a little more robust when the abc1234 is not the only thing on a
    line.

    Josef
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Möllers, Oct 6, 2003
    #3
  4. François

    Helgi Briem Guest

    On Mon, 6 Oct 2003 15:23:44 +0200, "François" <> wrote:

    >I want to replace an expression abc1234 to abc-1234 how can i do
    >I 've tried
    >
    >s/......./$1$2$3-$4$5$6$7/ but it dont work.


    I presume the format is originally
    3 letters 4 numbers?

    If so:

    s/(^[a-z]{3})(\d{4})$/$1-$2/;
     
    Helgi Briem, Oct 6, 2003
    #4
  5. Bernard El-Hagin wrote:
    >
    > Josef Möllers <> wrote in
    > news::
    >
    > > Bernard El-Hagin wrote:
    > >>

    > >
    > >> "François" <> wrote in news:blrqe4$i6a$1
    > >> @s1.read.news.oleane.net:
    > >>

    > >
    > >> > I want to replace an expression abc1234 to abc-1234 how can i do
    > >> > I 've tried
    > >> >
    > >> > s/......./$1$2$3-$4$5$6$7/ but it dont work.
    > >> >
    > >> > Please help ?
    > >>

    > >
    > >> s/(\D+)(\d+)/$1-$2/

    > >
    > > TMTOWTDI
    > >
    > > I'd use
    > > s/(\D{3})(\d{4})/$1-$2/
    > >
    > > It's a little more robust when the abc1234 is not the only thing on a
    > > line.

    >
    > s/abc1234/abc-1234/ is even more robust. ;-)


    Why didn't I think of that B-{)))

    > That's the problem with general specifications of problems.


    Yes, you're right.
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
     
    Josef Möllers, Oct 6, 2003
    #5
  6. François

    François Guest

    thanks that work

    "Helgi Briem" <> a écrit dans le message de
    news:...
    > On Mon, 6 Oct 2003 15:23:44 +0200, "François" <> wrote:
    >
    > >I want to replace an expression abc1234 to abc-1234 how can i do
    > >I 've tried
    > >
    > >s/......./$1$2$3-$4$5$6$7/ but it dont work.

    >
    > I presume the format is originally
    > 3 letters 4 numbers?
    >
    > If so:
    >
    > s/(^[a-z]{3})(\d{4})$/$1-$2/;
    >
     
    François, Oct 6, 2003
    #6
  7. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    "François" <> wrote in news:blrqe4$i6a$1
    @s1.read.news.oleane.net:

    > I want to replace an expression abc1234 to abc-1234 how can i do
    > I 've tried
    >
    > s/......./$1$2$3-$4$5$6$7/ but it dont work.
    >
    > Please help ?


    In order for $1 (etc) to work, the left-hand side of the expression needs
    to have parentheses in it. For example:

    s/(.)(.)(.)(.)(.)(.)(.)/$1$2$3-$4$5$6$7/

    should do the trick for you.

    The following:

    s/(...)(....)/$1-$2/

    does the same thing (effectively), but is shorter and easier to write.

    - --
    Eric
    $_ = reverse sort $ /. r , qw p ekca lre uJ reh
    ts p , map $ _. $ " , qw e p h tona e and print

    -----BEGIN PGP SIGNATURE-----
    Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

    iQA/AwUBP4IFtWPeouIeTNHoEQKECgCgwuDkBaMvCKw/oyw2eo7TCgcd1W4AoM2U
    /nwmtz5QEnAVUVQjogosdg0F
    =Du8q
    -----END PGP SIGNATURE-----
     
    Eric J. Roode, Oct 7, 2003
    #7
  8. >>>>> "François" == François <> writes:

    François> I want to replace an expression abc1234 to abc-1234 how can i do
    François> I 've tried

    François> s/......./$1$2$3-$4$5$6$7/ but it dont work.

    s/abc1234/abc-1234/ would do it, as would $_ = "abc-1234", but since
    you've given only one example and no rules, it's hard to generalize
    that.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
     
    Randal L. Schwartz, Oct 7, 2003
    #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. Replies:
    0
    Views:
    691
  2. Larry Kim
    Replies:
    0
    Views:
    921
    Larry Kim
    Sep 2, 2003
  3. Akmal B. Chaudhri
    Replies:
    0
    Views:
    709
    Akmal B. Chaudhri
    Jul 11, 2004
  4. Brett C.
    Replies:
    7
    Views:
    343
    Michael Hudson
    Feb 26, 2004
  5. Brett C.
    Replies:
    0
    Views:
    260
    Brett C.
    Apr 18, 2004
Loading...

Share This Page