Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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.

Arjen


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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2015-07-08 08:32 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 (96 votes), past polls