Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Regex to add space after punctuation sign

by Aragorn (Curate)
on Jan 08, 2004 at 09:23 UTC ( #319760=note: print w/ replies, xml ) Need Help??

in reply to Regex to add space after punctuation sign

I found a solution without using look-behind, which has the limitation of not being able to match variable length strings (so /(?<!\t*)foo)/ does not work).

#!/usr/local/bin/perl use strict; while (my $str = <DATA>) { chomp($str); $str =~ s/(\d+[.,]\d+ # Substitute a (simple) number ... | # or ... [^.,\d ]+) # something without periods, comma's, # digits or spaces ... ((?!$)[.,]?) # followed by a period or comma, if # possible, but not when at the end of # the line ... /$1$2 /gx; # by the number or string, followed by # the found period or comma, and a # trailing space print "<$str>\n"; } __DATA__ 1,2,a,b a,1,b,2, 1,21,a,b,word,d.3,4.5.6 __END__ <1,2, a, b> <a, 1,b, 2,> <1,21, a, b, word, d. 3,4. 5.6>

In addition to the on-line documentation, you could check out Mastering Regular Expressions, 2nd Edition.


Comment on Re: Regex to add space after punctuation sign
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2015-11-26 01:13 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (695 votes), past polls