Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > HOW TO replace ' but not ?'

Reply
Thread Tools

HOW TO replace ' but not ?'

 
 
tor
Guest
Posts: n/a
 
      10-27-2004
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


 
Reply With Quote
 
 
 
 
Peter Hickman
Guest
Posts: n/a
 
      10-27-2004
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
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      10-27-2004
tor <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-27-2004
"Peter Hickman" <(E-Mail Removed)> wrote in message
news:417fa628$0$505$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Whitey Johnson
Guest
Posts: n/a
 
      10-27-2004
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";





 
Reply With Quote
 
Whitey Johnson
Guest
Posts: n/a
 
      10-27-2004
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";


 
Reply With Quote
 
Whitey Johnson
Guest
Posts: n/a
 
      10-27-2004
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";


 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-27-2004
"Whitey Johnson" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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


 
Reply With Quote
 
Whitey Johnson
Guest
Posts: n/a
 
      10-28-2004
On Wed, 27 Oct 2004 17:45:44 +0000, Paul Lalli muttered incoherently:

> "Whitey Johnson" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed)...

<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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Replace /n with a XHTML <br /> using string.replace Alun ASP .Net 3 02-18-2008 05:52 AM
Re: [Pyrex] pyrex functions to replace a method (Re: replace a method Greg Ewing Python 2 06-29-2006 05:25 PM
pyrex functions to replace a method (Re: replace a method in class:how?) Brian Blais Python 1 06-27-2006 12:13 PM
help with string replace - for doing selective replace Prasad S Javascript 2 08-27-2004 03:22 PM



Advertisments