Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Space between digits

by Anonymous Monk
on Nov 21, 2012 at 21:17 UTC ( #1005011=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi, I am new to perl. I want to know if there is any way to insert space between two numeric patterns. like
0:00 11/21/2012
I need triple space instead of one space between 00 and 11 Thank you

Replies are listed 'Best First'.
Re: Space between digits
by tobyink (Abbot) on Nov 21, 2012 at 21:23 UTC
    use 5.014; my $string = "0:00 11/21/2012"; my $new = $string =~ s/ / /r; say $string; say $new;
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      I meant for that pattern and of date and time. I didnt mean that particular string. for example for the pattern
      (\d+)\:(\d+) (\d+)\/(\d+)\/(\d+)
      how do I triple the space between the time and date ?

        The code I posted will work on any string. It finds the first space and replaces it with three spaces.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Space between digits
by bart (Canon) on Nov 23, 2012 at 12:29 UTC
    This might help:
    s/(?<=\d) (?=\d)/ /
    It replaces a single space with 3 provided there's a digit (at least one) in front of it, and one (at least one) behind it.
      Yes yes that was good one Bart. I tried that and it worked...thank you very much.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005011]
Approved by tobyink
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2018-02-23 09:28 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (301 votes). Check out past polls.