HOW TO replace ' but not ?'

T

tor

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
 
P

Peter Hickman

tor said:
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
 
T

Tad McClellan

tor said:
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;
 
P

Paul Lalli

Peter Hickman said:
tor said:
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
 
W

Whitey Johnson

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";
 
W

Whitey Johnson

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

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;
 
W

Whitey Johnson

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

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?
 
W

Whitey Johnson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top