Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Something like:
$s =~ s/(?<=(?<!\d(?=[.,]\d))\p{P})(?!\s)/ /g;
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:

$ perl -wle'use charnames (); for (0..255) { chr($_) =~ /\pP/ and prin +t chr($_) ," ($_): ", charnames::viacode($_) }' ! (33): EXCLAMATION MARK " (34): QUOTATION MARK # (35): NUMBER SIGN % (37): PERCENT SIGN & (38): AMPERSAND ' (39): APOSTROPHE ( (40): LEFT PARENTHESIS ) (41): RIGHT PARENTHESIS * (42): ASTERISK , (44): COMMA - (45): HYPHEN-MINUS . (46): FULL STOP / (47): SOLIDUS : (58): COLON ; (59): SEMICOLON ? (63): QUESTION MARK @ (64): COMMERCIAL AT [ (91): LEFT SQUARE BRACKET \ (92): REVERSE SOLIDUS ] (93): RIGHT SQUARE BRACKET _ (95): LOW LINE { (123): LEFT CURLY BRACKET } (125): RIGHT CURLY BRACKET (161): INVERTED EXCLAMATION MARK (171): LEFT-POINTING DOUBLE ANGLE QUOTATION MARK (183): MIDDLE DOT (187): RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK (191): INVERTED QUESTION MARK
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 reply to Re: Regex to add space after punctuation sign by ysth
in thread Regex to add space after punctuation sign by dda

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (10)
    As of 2014-12-26 09:25 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (171 votes), past polls