Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    [ovedpo15]: someone has experience with SlickEdit editor? I have it on Linux and I would like to debug my perl code from the shell. what is the commend to run it using the debbuger?

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (5)
    As of 2018-05-25 18:24 GMT
    Find Nodes?
      Voting Booth?