[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] help?



Well, that was fun. Is this what you're looking for?

$i is the index of the word. Whatever comes between the $1 and the $4 in
the replacement string is what you're replacing it with. In the
following case, I just replace the "words" in sequence with an asterisk.
When $i = 1, word no. 1 gets replaced, etc. The word to be replaced is
in $3, so to append to it rather than replace it, use $1$3<string>$4 as
in $1$3ing$4 to append 'ing'.

Remember that if you have carriage returns and such in your string, you
should replace the '\S', '\W' and '.' shortcuts with explicit ranges,
i.e. [\x00-\xFF] for '.', etc.

#!perl
$yarn = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
for ( $i = 0; $i < 26; $i++ ) {
    $yarn =~ s/((\W?\S+\W?){$i})(\S+)(.*)/$1*$4/;
    print "$yarn\n";
}
exit(0);

Yields:

* b c d e f g h i j k l m n o p q r s t u v w x y z
* * c d e f g h i j k l m n o p q r s t u v w x y z
* * * d e f g h i j k l m n o p q r s t u v w x y z
* * * * e f g h i j k l m n o p q r s t u v w x y z
* * * * * f g h i j k l m n o p q r s t u v w x y z
* * * * * * g h i j k l m n o p q r s t u v w x y z
* * * * * * * h i j k l m n o p q r s t u v w x y z
* * * * * * * * i j k l m n o p q r s t u v w x y z
* * * * * * * * * j k l m n o p q r s t u v w x y z
* * * * * * * * * * k l m n o p q r s t u v w x y z
* * * * * * * * * * * l m n o p q r s t u v w x y z
* * * * * * * * * * * * m n o p q r s t u v w x y z
* * * * * * * * * * * * * n o p q r s t u v w x y z
* * * * * * * * * * * * * * o p q r s t u v w x y z
* * * * * * * * * * * * * * * p q r s t u v w x y z
* * * * * * * * * * * * * * * * q r s t u v w x y z
* * * * * * * * * * * * * * * * * r s t u v w x y z
* * * * * * * * * * * * * * * * * * s t u v w x y z
* * * * * * * * * * * * * * * * * * * t u v w x y z
* * * * * * * * * * * * * * * * * * * * u v w x y z
* * * * * * * * * * * * * * * * * * * * * v w x y z
* * * * * * * * * * * * * * * * * * * * * * w x y z
* * * * * * * * * * * * * * * * * * * * * * * x y z
* * * * * * * * * * * * * * * * * * * * * * * * y z
* * * * * * * * * * * * * * * * * * * * * * * * * z
* * * * * * * * * * * * * * * * * * * * * * * * * *

Robert

Kevin Newman wrote:
> I'm trying to figure out a regular expression, and any help I could get
> would be greatly appreciated. What I'm trying to do is manipulate a word
> in string. Not any word, mind you, but sometimes the first, other times
> the fifteenth, another time the eleventh. Finding the word and it's
> 'number' in a string is fine, but getting to it via a regexp is my
> problem.



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch