[ale] regexp question

David Hamm ale at spinnerdog.com
Wed Oct 1 17:58:15 EDT 2003


I have the book and the example is much more elaborate than I need. Here
is an example.
The string I have: Now is the time
The string I want: Now is#the time
I only need to replace the second occurrence with a #.

David Hamm
Spinnerdog Inc
#250 1029 Peachtree Parkway
Peachtree City, GA 30269
www.spinnerdog.com
[voice] 770-631-1329
[fax] 770-632-0613
 
 

-----Original Message-----
From: ale-bounces at ale.org [mailto:ale-bounces at ale.org] On Behalf Of
Taylor Robison
Sent: Wednesday, October 01, 2003 1:27 PM
To: Atlanta Linux Enthusiasts
Subject: Re: [ale] regexp question



Perl Cookbook
by Tom Christiansen and Nathan Torkington
O'Reilly
See page 170.
Great book.

#!/usr/bin/perl

$_ = "One fish two fish red fish blue fish";

$WANT = 3;
$count = 0;
while (/(\w+)\s+fish\b/gi) {
if (++$count == $WANT) {
print "The third fish is a $1 one.\n";
# warning: don't 'last' out of this loop
}
}



David Hamm wrote:

> I can't remember how to replace the nth occurrence of a character in a

> string using a regular expression and I can't seem to find docs that 
> will jog my memory. Could someone help me out?
>
>-----------------------------------------------------------------------
-
>
>_______________________________________________
>Ale mailing list
>Ale at ale.org
>http://www.ale.org/mailman/listinfo/ale
>  
>

_______________________________________________
Ale mailing list
Ale at ale.org
http://www.ale.org/mailman/listinfo/ale



More information about the Ale mailing list