Perl: the Markov chain saw | |
PerlMonks |
Re: Regex to add space after punctuation signby ysth (Canon) |
on Jan 08, 2004 at 09:19 UTC ( [id://319757]=note: print w/replies, xml ) | Need Help?? |
Something like:
Taking that apart, we are looking for a spot between two characters where a space should be inserted. We have a positive requirement for what comes before the spot (that's the (?<=...)) and a negative requirement for what comes after the spot. The after condition is simple. If there is a whitespace character (I'm guessing that you mean to include tab, newline, etc., based on your use of \S) this spot can be skipped. The before condition is essentially (?<=\pP) with an additional condition. \pP matches any punctuation character. These are: Plus several more unicode characters if you have utf8 data. If you mean a more restricted set of characters, use a character class like [.!,;] or whatever. The additional condition is (?<!\d(?=[.,]\d)): this stipulates that what comes before the punctuation should not match \d(?=[.,]\d); that is a single digit, where the digit is followed by a period or comma and another digit. Note that that lookahead is actually escaping the bounds of the outer lookbehind, but that is perfectly ok to do. Update: added actual character to punctuation table Update: weird. backtick (GRAVE ACCENT) isn't considered punctuation
In Section
Seekers of Perl Wisdom
|
|