Re: how to add a space using a regex



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
.



Relevant Pages

  • Re: How to change the first character of Dim variable names to upper case
    ... I just don't know how to upper case the replacement group. ... Do you mean the IDE dialect is different from RegEx? ... Doing somethings in RegEx is differsnt than doing it in the IDE? ... I realized things like excluding prefix/suffice matches ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to change the first character of Dim variable names to upper case
    ... I just don't know how to upper case the replacement group. ... Doing somethings in RegEx is differsnt than doing it in the IDE? ... I realized things like excluding prefix/suffice matches ... sometimes I mistakenly use upper case so I'd like to fix all those mistakes ...
    (microsoft.public.dotnet.languages.vb)
  • Regex for password policy
    ... how can I merge these requirements into one regex? ... string shall at least have 8 chars ... at least one upper case letter should be contained ...
    (comp.lang.java.programmer)
  • Re: How to change the first character of Dim variable names to upper case
    ... I just don't know how to upper case the replacement group. ... Doing somethings in RegEx is differsnt than doing it in the IDE? ... I realized things like excluding prefix/suffice matches ... sometimes I mistakenly use upper case so I'd like to fix all those mistakes ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to change the first character of Dim variable names to upper case
    ... I just don't know how to upper case the replacement group. ... Doing somethings in RegEx is differsnt than doing it in the IDE? ... I realized things like excluding prefix/suffice matches ...
    (microsoft.public.dotnet.languages.vb)