Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Regex to add space after punctuation sign

by Abigail-II (Bishop)
on Jan 08, 2004 at 09:20 UTC ( #319758=note: print w/ replies, xml ) Need Help??


in reply to Regex to add space after punctuation sign

Previous solutions only considered commas and periods, not other punctuation symbols. Assuming anything that's not a word character or whitespace is to be considered punctuation, I would use:

s/(\d[,.]\d)|([^\w\s](?!\s))/$1 || "$2 "/ge;

Abigail


Comment on Re: Regex to add space after punctuation sign
Download Code
Replies are listed 'Best First'.
Re: Re: Regex to add space after punctuation sign
by dda (Friar) on Jan 08, 2004 at 09:52 UTC
    Thank you, Abigail. Can you please explain how does the replacement part of your expression work? If I understand correctly, /e evaluates $1 || "$2 " -- that way if $1 is not empty, it should not add second part?

    --dda

    P.S. I added end-of-line processings as was suggested by aragorn:
    s/(\d[,.]\d)|([^\w\s](?!\s)(?!$))/$1 || "$2 "/ge;
      Evaluating $1 || "$2 " means you get $1 if $1 is true (which means, anytime \d[.,]\d matches), otherwise you get $2 (the punctation character), followed by a space.

      I would handle end-of-line processing as follows:

      s/(\d[,.]\d)|([^\w\s])(?=\S)/$1 || "$2 "/ge;
      A single positive look-ahead is, IMO, more clear than two negative look-aheads.

      Abigail

        Thank you. You are the best!

        --dda

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://319758]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (13)
As of 2015-07-31 19:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (280 votes), past polls