in reply to Re: Deleting intermediate whitespaces, but leaving one behind each word
in thread Deleting intermediate whitespaces, but leaving one behind each word

Yes, johngg,

TIMTOWTDI, and, yes, I think this is definitely of interest++. Having said that, I feel that using zero-width look-around assertions for such a simple case might be a little bit of an overkill. Well, at least for a beginner who obviously doesn't know very much about regexes at this point.

Personally, I'm using look-around assertions only from time to time (sometimes, it is really the best solution), but not often enough to always remember the exact syntax by heart, so that when I feel this is the right solution, I usually have to look it up in Johan Vromans's Perl Pocket Reference (or on the net or somewhere else). For such a simple case, I would rather do most of the job with a simple s/\s+/ /g regex, and add one or two simple regexes to handle leading and trailing spaces if needed. My colleagues having to maintain my code will probably thank me for that and I will be even more delighted when the person having to maintain this code a year from now will be... me.

BTW, Perl 6's regexes have a much cleaner syntax for look-around assertions, so that I would not have the same second thoughts in P6. But that's getting slightly OT, sorry for that.

  • Comment on Re^2: Deleting intermediate whitespaces, but leaving one behind each word
  • Download Code