Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Use Regular Expression to add spaces to a string

by omegaweaponZ (Beadle)
on Mar 26, 2012 at 20:46 UTC ( #961743=perlquestion: print w/replies, xml ) Need Help??
omegaweaponZ has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I'm still a newbie when it comes to regular expressions. I'm OK with matching and replacing strings, but I'm trying to use re's to take a single string and add spaces to it. See example below:

$oldstring = '123456789'

-- regular expression function --

$newstring = '123 456-789'

Any ideas? I'm assuming it's based on counting characters based on .. as those characters will constantly change, but the order in which I need them to add a space or - will not

  • Comment on Use Regular Expression to add spaces to a string

Replies are listed 'Best First'.
Re: Use Regular Expression to add spaces to a string
by BrowserUk (Pope) on Mar 26, 2012 at 20:54 UTC
    Any ideas?

    The obvious way:

    $oldstring = '123456789';; $oldstring =~ s[(...)(...)(...)][$1 $2-$3];; print $oldstring;; 123 456-789

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Winner goes to BrowserUk. This is extremely simple and intuitive. Does exactly what I need and is the easiest to form. Thanks!
Re: Use Regular Expression to add spaces to a string
by petdance (Parson) on Mar 26, 2012 at 21:42 UTC
    Part of what you MUST do when writing a regex is to describe the transformation you want in English. An example is not enough. You might see the transformation as obvious, but we can't, and your regex certainly can't tell what you're thinking.

    It seems that what you're wanting to do is this: "Take a nine-character string and change it into the first three characters, followed by a space, followed by the second three characters, followed by a hyphen, followed by the last three characters."

    If that's what you want, you write it like this.

    $str =~ s/^(...)(...)(...)$/$1 $2-$3/';
    But that will also translate "bookstore" into "boo kst-ore". Is that OK? Or do you only want to do transformations if there are digits? Then you have to write it as:
    $str =~ s/^(\d\d\d)(\d\d\d)(\d\d\d)$/$1 $2-$3/';


Re: Use Regular Expression to add spaces to a string
by johngg (Abbot) on Mar 26, 2012 at 21:54 UTC

    Just to show another way that doesn't use regular expressions, you could use substr with a negative offset to work back from the end of the string using a loop statement modifier. Not as clear to the eye as BrowserUk's regex solution but hopefully of interest.

    knoppix@Microknoppix:~$ perl -E ' > $str = q{abcdefghi}; > substr $str, -( $_ + 3 * ( $_ + 1 ) ), 0, ( q{-}, q{ } )[ $_ ] > for 0 .. 1; > say $str;' abc def-ghi knoppix@Microknoppix:~$



Re: Use Regular Expression to add spaces to a string
by JavaFan (Canon) on Mar 26, 2012 at 23:23 UTC
    ($newstring = $oldstring) =~ s/456/ 456-/;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2018-02-18 02:12 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (250 votes). Check out past polls.