Add Whitespace after Commas

by Dec 20, 2010

Operator replace is extremely powerful when you use back references. For example, to make sure in a text that there is a space after each comma, you can try this:

'After each comma,I want a whitespace,but only if there was no whitespace in the first place, `
  of course.' -replace ',(\S)', ', $1'

The regular expression is looking for any occurrences of a comma followed immediately by non-whitespace (/S). If found, replaces it with a comma, a space and whatever the non-whitespace character was (the match in parenthesis, represented by the back reference in $1).

Twitter This Tip!
ReTweet this Tip!