For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > October 2004 > HOW TO replace ' but not ?'









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author HOW TO replace ' but not ?'
tor

2004-10-27, 3:57 pm

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


Peter Hickman

2004-10-27, 3:57 pm

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
bengee

2004-10-27, 3:57 pm

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'


Hi Tom. Done a bit of EDIFACT in my time! You need to use a regexp to
replace "[not a question mark]'" so try this :-

my $var = "AAA+Tim?'s'BBB+123'";
print("BEFORE: $var\n");
$var =~ s/([^\?]')/$1\n/g;
print("AFTER : $var\n");

HTH

Ben
bengee

2004-10-27, 3:57 pm

bengee wrote:
> Hi Tom. Done a bit of EDIFACT in my time! You need to use a regexp to
> replace "[not a question mark]'" so try this :-
>
> my $var = "AAA+Tim?'s'BBB+123'";
> print("BEFORE: $var\n");
> $var =~ s/([^\?]')/$1\n/g;
> print("AFTER : $var\n");


IIRC is the escape char in EDIFACT and to get a ? you need to escape
itself, i.e. ?? So what about this :-

AAA+Tim OK??'s'BBB+123'

I'll leave you to work that one out as an exercise ;-)

bengee
Bernard El-Hagin

2004-10-27, 3:57 pm

"tor" <torfinnk@hotmail.com> 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'



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


--
Cheers,
Bernard
Tad McClellan

2004-10-27, 3:57 pm

tor <torfinnk@hotmail.com> 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
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Paul Lalli

2004-10-27, 3:57 pm

"Peter Hickman" <peter@semantico.com> wrote in message
news:417fa628$0$505$afc38c87@news.easynet.co.uk...
> tor wrote:
>
> 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


Whitey Johnson

2004-10-27, 3:57 pm

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

2004-10-27, 3:57 pm

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

2004-10-27, 3:57 pm

On Wed, 27 Oct 2004 11:39:41 -0500, Whitey Johnson muttered incoherently:

<snip snip>
>
> 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?
[color=darkred]
>

Paul Lalli

2004-10-27, 3:57 pm

"Whitey Johnson" <whitey@newmail.net> wrote in message
news:pan.2004.10.27.17.34.09.442818@newmail.net...
> On Wed, 27 Oct 2004 11:39:41 -0500, Whitey Johnson muttered

incoherently:
>
> <snip snip>
>
> 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


Abigail

2004-10-27, 8:56 pm

Whitey Johnson (whitey@newmail.net) wrote on MMMMLXXV September MCMXCIII
in <URL:news:pan.2004.10.27.17.34.09.442818@newmail.net>:
:} 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. It won't work, or won't work correctly, in the following cases:

* Strings that start with a quote - it will fail to put a newline
after the leading quote.
* Strings that have two or more quotes in a row - it will only put
a newline after the first, third, fifth, ..., quote.


Abigail
--
srand 123456;$-=rand$_--=>@[[$-,$_]=@[[$_,$-]for(reverse+1..(@[=split
//=>"IGrACVGQ\x02GJCWVhP\x02PL\x02jNMP"));print+(map{$_^q^"^}@[),"\n"
Whitey Johnson

2004-10-28, 3:59 pm

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

> "Whitey Johnson" <whitey@newmail.net> wrote in message
> news:pan.2004.10.27.17.34.09.442818@newmail.net...

<snip>
>
> 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.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com