makar has asked for the wisdom of the Perl Monks concerning the following question:
The regex is a reference to earlier in the chapter, a duplicate word killer. After playing around with test patterns and test regex searches, I am not sure I really get what's going on above (perhaps it is just a lot to hit a weekend programmer like myself with at once ^_^). Here is what I think is going on: 1st regex) The while loop has a one because all the interesting stuff happens in the logical test. (\b(\w+) \1\b) matches a word boundary followed by one or more word characters followed by a space and whatever matched the (\w+). The match of (\w+) replaces the whole match. I think I kinda get it.$_ = "Paris in THE THE THE THE spring"; 1 while s/\b(\w+) \1\b/$1/gi;
Unfortunately I'm not really good enough to divine the difference between the two. Also, I don't see why the substitution that the book does doesn't eat up the preceding space: "Paris THE THE" turns into "ParisTHE". I hope I've a real conceptual problem here, and not just a brain fart, because I'll be feeling really sheepish asking if it is indeed a brain fart. Thanks in advance!/(\b\w+)\1\b
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Prog. Perl 3rd Ed. Regex Question
by NetWallah (Canon) on Feb 13, 2004 at 05:16 UTC | |
by makar (Novice) on Feb 13, 2004 at 05:43 UTC | |
by davido (Cardinal) on Feb 13, 2004 at 06:02 UTC | |
|
Re: Prog. Perl 3rd Ed. Regex Question
by japhy (Canon) on Feb 13, 2004 at 06:02 UTC | |
|
Re: Prog. Perl 3rd Ed. Regex Question
by leriksen (Curate) on Feb 13, 2004 at 07:20 UTC | |
|
Re: Prog. Perl 3rd Ed. Regex Question
by artist (Parson) on Feb 13, 2004 at 05:23 UTC | |
|
Re: Prog. Perl 3rd Ed. Regex Question
by Anonymous Monk on Feb 13, 2004 at 08:20 UTC |