From: eldwin on
Hey all,

I'm trying to search and replace to the following string:

0|1061203|150200559802-12345|STUDIO
0006979867|STUDIO|0066678||STUDIO|150200559802-12345|STAFF|XX|STAFF|XX|

What I want to do is replace whatever is in between | |.

For example, I want to replace STAFF with PROSTAFF.

my current regex looks like this:

s/\|.*\|XX\|/\|PROSTAFF\|XX\|/g

So, I'm not concerned with STAFF so much, but what's in between | |. I
know "|" is a special character thus the "\" before it.

When I apply this regex to my code, the resulting string is:

0|PROSTAFF|XX|

Thanks in advance for any advice.

From: Jim Gibson on
In article <1169073854.097814.163470(a)51g2000cwl.googlegroups.com>,
eldwin <dollente(a)gmail.com> wrote:

> Hey all,
>
> I'm trying to search and replace to the following string:
>
> 0|1061203|150200559802-12345|STUDIO
> 0006979867|STUDIO|0066678||STUDIO|150200559802-12345|STAFF|XX|STAFF|XX|
>
> What I want to do is replace whatever is in between | |.

Which pair of '|'? There are more than one.

>
> For example, I want to replace STAFF with PROSTAFF.
>
> my current regex looks like this:
>
> s/\|.*\|XX\|/\|PROSTAFF\|XX\|/g

..* is a greedy match to all characters in the string, including '|'
characters. You need to make it non-greedy (.*?) or match non-pipe
characters ([^|]*).

'PROSTAFF' is not in your string. Do you mean 'STAFF'?

>
> So, I'm not concerned with STAFF so much, but what's in between | |. I
> know "|" is a special character thus the "\" before it.
>
> When I apply this regex to my code, the resulting string is:
>
> 0|PROSTAFF|XX|

Another approach is to use split to break up your line into the
substrings that occur between pipe characters, modify every other
substring, and put the line back together (untested):

my @f = split(m{\|},$string);
for( my $i = 1; $i <= $#f; $i += 2 ) {
$f[$i] =~ s/^STAFF$/PROSTAFF/;
}
my $newstring = join('|',@f);

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
From: Josef Moellers on
eldwin wrote:
> Hey all,
>
> I'm trying to search and replace to the following string:
>
> 0|1061203|150200559802-12345|STUDIO
> 0006979867|STUDIO|0066678||STUDIO|150200559802-12345|STAFF|XX|STAFF|XX|
>
> What I want to do is replace whatever is in between | |.
>
> For example, I want to replace STAFF with PROSTAFF.
>
> my current regex looks like this:
>
> s/\|.*\|XX\|/\|PROSTAFF\|XX\|/g
>
> So, I'm not concerned with STAFF so much, but what's in between | |. I
> know "|" is a special character thus the "\" before it.
>
> When I apply this regex to my code, the resulting string is:
>
> 0|PROSTAFF|XX|
>
> Thanks in advance for any advice.

You may want to take a look at Text::CSV_XS

use warnings;
use strict;
use Text::CSV_XS;

my $csv = Text::CSV_XS->new({sep_char => '|'});
die "failed to get new csv\n" unless $csv;
while (<STDIN>) {
chomp;
my $status = $csv->parse($_);
die "bad line $_\n" unless $status;
my @fields = $csv->fields();
foreach (@fields) {
$_ = "PROSTAFF" if $_ eq "STAFF";
}
$status = $csv->combine(@fields);
die "failed to combine ", join('|', @fields), "\n" unless $status;
print $csv->string(), "\n";

}

--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

From: Michele Dondi on
On Wed, 3 Jan 2007 15:32:37 +0100, "Andrea Petersen" <andreap(a)gmx.de>
wrote:

>References: <engcg7$jtm$00$1(a)news.t-online.com> <1167834145.506596.249160(a)k21g2000cwa.googlegroups.com>

Since this is a followup, *PLEASE* quote some context from the post
you're replying to.

>My problem:
>
><div>[a-z0-9\,\<\>\/ ]*</div>
>
>does not match the first div-tag. (but matches the second)

Did you actually *read* the post you're replying to? I don't see how
this comment of yours may apply to any part of it. Indeed you've been
recommended not to use regexen for a task like this, i.e. one for
which they aren't well suited to start from, and use an appropriate
module instead.

>my somach hurts. regexs are very unhealthy.

Regexen are perfectly healthy if you use them for what they're
actually good for. Parsing HTML is just not such a thing. You have to
live with this.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
From: eldwin on
> > 0|PROSTAFF|XX|Another approach is to use split to break up your line into the
> substrings that occur between pipe characters, modify every other
> substring, and put the line back together (untested):
>
> my @f = split(m{\|},$string);
> for( my $i = 1; $i <= $#f; $i += 2 ) {
> $f[$i] =~ s/^STAFF$/PROSTAFF/;
> }
> my $newstring = join('|',@f);
>

The split function did the trick. Thanks for your help.