Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Use Regular Expression to add spaces to a string

by petdance (Parson)
on Mar 26, 2012 at 21:42 UTC ( #961762=note: print w/replies, xml ) Need Help??

in reply to Use Regular Expression to add spaces to a string

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/';


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://961762]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2017-01-18 07:31 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (160 votes). Check out past polls.