HOW TO replace ' but not ?'

Discussion in 'Perl Misc' started by tor, Oct 27, 2004.

  1. tor

    tor Guest

    Hello
    I want to put a new line after each ', but not after ?'.
    If someone are familiare with edifact they understand.

    AAA+Tim?'s'BBB+123'

    should become

    AAA+Tim?'s'
    BBB+123'

    anyone???

    Torfinn
     
    tor, Oct 27, 2004
    #1
    1. Advertising

  2. tor wrote:
    > Hello
    > I want to put a new line after each ', but not after ?'.
    > If someone are familiare with edifact they understand.
    >
    > AAA+Tim?'s'BBB+123'
    >
    > should become
    >
    > AAA+Tim?'s'
    > BBB+123'
    >
    > anyone???
    >
    > Torfinn
    >
    >


    How about this?

    $a = "AAA+Tim?'s'BBB+123'";

    print "$a\n";

    $a =~ s/([^\?]')/$1\n/g;

    print "$a\n";

    The ([^\?]') says match and capture any character other than a ? that is
    followed by a '

    The $1\n says replace this with whatever you captured plus a newline
     
    Peter Hickman, Oct 27, 2004
    #2
    1. Advertising

  3. tor <> wrote:

    > I want to put a new line after each ', but not after ?'.



    > AAA+Tim?'s'BBB+123'
    >
    > should become
    >
    > AAA+Tim?'s'
    > BBB+123'
    >
    > anyone???



    s/(?<!\?)'/'\n/g;

    or more readably:

    s/(?<! \? ) # preceding char is not a question mark
    '
    /'\n/gx;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Oct 27, 2004
    #3
  4. tor

    Paul Lalli Guest

    "Peter Hickman" <> wrote in message
    news:417fa628$0$505$...
    > tor wrote:
    > > Hello
    > > I want to put a new line after each ', but not after ?'.
    > > If someone are familiare with edifact they understand.

    >
    > How about this?
    >
    > $a = "AAA+Tim?'s'BBB+123'";
    > print "$a\n";
    > $a =~ s/([^\?]')/$1\n/g;


    Question marks are not special in character classes. No need for a
    backslash there.

    > print "$a\n";
    >
    > The ([^\?]') says match and capture any character other than a ? that

    is
    > followed by a '
    >
    > The $1\n says replace this with whatever you captured plus a newline


    I'm not familiar with EDIFACT, so this may not apply. However, if there
    is a possibility that a ' could be the first (or only) character in the
    string, and you need to insert a newline after it, this will not work.
    The [^?] requires an actual character.

    To match just "an apostrophe not preceded by a question mark" (rather
    than "anything that's not a question mark, followed by an apostrophe"),
    you need to use negative lookbehind assertions:

    s/(?<!\?)'/'\n/;

    Paul Lalli
     
    Paul Lalli, Oct 27, 2004
    #4
  5. On Wed, 27 Oct 2004 15:30:23 +0200, tor muttered incoherently:

    > Hello
    > I want to put a new line after each ', but not after ?'.
    > If someone are familiare with edifact they understand.
    >
    > AAA+Tim?'s'BBB+123'
    >
    > should become
    >
    > AAA+Tim?'s'
    > BBB+123'
    >
    > anyone???
    >
    > Torfinn


    Ooooh oooh oooh!!
    I wanna give this a try before I look at the other respones:

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $line = "AAA+Tim?'s'BBB+123'";
    $line =~ s#[^?]'#'\n#;
    print $line. "\n";
     
    Whitey Johnson, Oct 27, 2004
    #5
  6. On Wed, 27 Oct 2004 11:36:27 -0500, Whitey Johnson muttered incoherently:

    <snip>
    >
    > Ooooh oooh oooh!!
    > I wanna give this a try before I look at the other respones:
    >
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > my $line = "AAA+Tim?'s'BBB+123'";
    > $line =~ s#[^?]'#'\n#;


    Damn, make that: $line =~ s#[^?]'#'\n#g;

    > print $line. "\n";
     
    Whitey Johnson, Oct 27, 2004
    #6
  7. On Wed, 27 Oct 2004 11:39:41 -0500, Whitey Johnson muttered incoherently:

    <snip snip>
    >>
    >> my $line = "AAA+Tim?'s'BBB+123'";
    >> $line =~ s#[^?]'#'\n#;

    >
    > Damn, make that: $line =~ s#[^?]'#'\n#g;


    One more try: $line =~ s#([^?])'#$1'\n#g;

    I read Tad's post and was wondering if there was a case where this
    wouldn't work?

    >
    >> print $line. "\n";
     
    Whitey Johnson, Oct 27, 2004
    #7
  8. tor

    Paul Lalli Guest

    "Whitey Johnson" <> wrote in message
    news:p...
    > On Wed, 27 Oct 2004 11:39:41 -0500, Whitey Johnson muttered

    incoherently:
    >
    > <snip snip>
    > >>
    > >> my $line = "AAA+Tim?'s'BBB+123'";
    > >> $line =~ s#[^?]'#'\n#;

    > >
    > > Damn, make that: $line =~ s#[^?]'#'\n#g;

    >
    > One more try: $line =~ s#([^?])'#$1'\n#g;
    >
    > I read Tad's post and was wondering if there was a case where this
    > wouldn't work?
    >


    Yes. Read my post in this thread.

    Paul Lalli
     
    Paul Lalli, Oct 27, 2004
    #8
  9. On Wed, 27 Oct 2004 17:45:44 +0000, Paul Lalli muttered incoherently:

    > "Whitey Johnson" <> wrote in message
    > news:p...

    <snip>
    >> I read Tad's post and was wondering if there was a case where this
    >> wouldn't work?
    >>

    >
    > Yes. Read my post in this thread.
    >
    > Paul Lalli


    Thanks. I don't remember negative lookbehind assertions in the Llama book
    and it seems I am a couple of chapters away from them in the Camel book.
     
    Whitey Johnson, Oct 28, 2004
    #9
    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. Brian Blais
    Replies:
    1
    Views:
    400
    Bruno Desthuilliers
    Jun 27, 2006
  2. Greg Ewing
    Replies:
    2
    Views:
    366
    Dieter Maurer
    Jun 29, 2006
  3. Alun
    Replies:
    3
    Views:
    4,609
    Masudur
    Feb 18, 2008
  4. Bruno Desthuilliers
    Replies:
    12
    Views:
    381
    Bruno Desthuilliers
    Oct 15, 2008
  5. Prasad S
    Replies:
    2
    Views:
    250
    Dr John Stockton
    Aug 27, 2004
Loading...

Share This Page