Re: how to add a space using a regex
- From: Christian Winter <thepoet_nospam@xxxxxxxx>
- Date: Wed, 31 Aug 2005 10:05:58 +0200
Jerry Preston wrote:
What I want to do is add a space between one's name when the are like the following:
$name1 = "FirstLast:";
I want change $name1 = "First Last";
and
$name1 = "FirstMiddleLast:";
I want change $name1 = "First Middle Last";
To be able to add spaces for either "FirstLast:" or "FirstMiddleLast:" with the same regex?
You can use zero width look-behind and look-ahead assertions for that.
In words: Look for a place that is preceded by a lower case letter and followed by an upper case letter and substitute that place with a space.
Which looks like the following: $name1 =~ s/(?<=[a-z])(?=[A-Z])/ /g;
But you should have in mind that your method isn't fool proof. There may be surnames that contain more than one upper case letter (just ask Tad ;-)).
See "perldoc perlre" for details on zero width assertions.
HTH -Chris .
- References:
- how to add a space using a regex
- From: Jerry Preston
- how to add a space using a regex
- Prev by Date: Re: Is it possible to resize or crop JPG image with Perl?
- Next by Date: Re: how to add a space using a regex
- Previous by thread: how to add a space using a regex
- Next by thread: Re: how to add a space using a regex
- Index(es):
Relevant Pages
|
|