Gossamer Forum
Home : General : Perl Programming :

regexp question

Quote Reply
regexp question
Small regexp question:

Code:
my $string = q|a@alkdjsf ks dlkja@ <a href="mailto:aaaaa@somehost.com">Jack Jones</a>sdlkfj klsdj a@sldkfj a@sdkf a@|;


I'd like to replace all occurrences of "a@" to "A" except for the one inside the mailto-link.

Is there some quick regexp magic for doing such things?



Thx,
kellner

Last edited by:

kellner: Jul 25, 2002, 2:37 AM
Quote Reply
Re: [kellner] regexp question In reply to
Cheap and dirty, but is this kind of what you want?

Code:
my $str = join "", map { /href\s+=\s+"?mailto:\w+$/ ? '@' : 'A' } split /\@/, $string;
Quote Reply
Re: [Aki] regexp question In reply to
I'm not sure that works Aki....it outputs...

AAAAAA

I read it as it should change a@ to A but still keep everything else?

Last edited by:

Paul: Jul 25, 2002, 4:16 PM
Quote Reply
Re: [Aki] regexp question In reply to
My feeble effort....(its 12.40am and I'm not really in the mood :) )

Code:
my $string = q|a@alkdjsf ks dlkja@ <a href="mailto:aaaaa@somehost.com">Jack Jones</a>sdlkfj klsdj a@sldkfj a@sdkf a@|;
my $output;
for (split /@/, $string) {
if (substr($_, -1, 1) eq 'a' and index($_, 'mailto:') == -1) {
s/a$/A/;
$output .= $_;
next;
}
$output .= $_ . '@';
}
Quote Reply
Re: [Paul] regexp question In reply to
thanks, paul, I'll give it a try, and - yes, you're right, the point was to change every "a@" OUTSIDE pointed brackets into "A".

Changing content INSIDE pointed brackets seems quite easy to me, e.g. the following code works to change all double quotes to single quotes inside pointed brackets (i.e. inside html tags):

$string =~ s{ ( < [^>]+ > ) } { $a = $1; $a =~ tr|"|'|; $a }gex; # single quotes in html tag attributes

The question is how to do this in a negative way - NOT change every instance of some pattern inside pointed brackets, but change every instance outside.

As I said, I'll try Paul's code, and then let you know - thanks anyway!
kellner
Post deleted by Aki In reply to
Quote Reply
Re: [kellner] regexp question In reply to
$string =~ s/a@([^<]*[<])/A$1/g;
$string =~ s/([>][^>]*)a@/$1A/g;

err.. i have no idea what that's going to do.. but it seemed reasonable to me. :P


[edit]oh.. nm.. IGNORE me. that's too greedy.

[edit2]heres my solution..

while ($string =~ s/a@([^>]*[<])/A$1/g || $string =~ s/([>][^<]*)a@/$1A/g) {}

Last edited by:

widgetz: Jul 29, 2002, 7:14 PM