Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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.


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 avoiding work at the Monastery: (8)
As of 2016-10-21 20:25 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (289 votes). Check out past polls.