Re: how to add a space using a regex



Jerry Preston <g-preston1@xxxxxx> wrote in comp.lang.perl.misc:
> 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?

A regex doesn't change a string, but a substitution (one part of which
is a regex) does.

Can you insert spaces in either "FirstLast:" or "FirstMiddleLast:" with
the same substitution? That would depend on how you intend to recognize
the parts of the nname. You have said nothing about that.

Here is a substitution that inserts a space whereever a capital
letter immediately follows a lower-case one:

s/([[:lower:]])([[:upper:]])/$1 $2/g;

but that would mis-handle names like "McBarren".

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
.



Relevant Pages

  • Re: Space (s) count problem
    ... > certain string was found in another string. ... > It had to be fast, and after much benchmarking, I found the fastest method ... An alternative that avoids the unnecessary substitution is ... "Reply" at the bottom of the article headers. ...
    (comp.lang.perl.misc)
  • Re: how to add a space using a regex
    ... > anno4000@xxxxxxxxxxxxxxxxxxxxxxx (Anno Siegel) wrote: ... >> which is a regex) does. ... >> with the same substitution? ... > I suggest that two letter first names are quite rare, ...
    (comp.lang.perl.misc)
  • Re: how to add a space using a regex
    ... anno4000@xxxxxxxxxxxxxxxxxxxxxxx (Anno Siegel) wrote: ... > which is a regex) does. ... > with the same substitution? ... closer. ...
    (comp.lang.perl.misc)
  • Re: file renamer... request feedback
    ... So what your saying is that after the first substitution in global context ... you'd also have to tell the regex engine to drop the string it ... Since you may have designed the regex engine, can you tell me what regex is ... so that I do *NOT* write invalid regex? ...
    (comp.lang.perl.misc)
  • RE: Newbie: regular expression
    ... > The problem is that the second part of the substitution is not ... > taken as a regex, but as a string, which is to mean that you're trying ... If you want to strip more than the \n, a regex would be more appropriate, ... but if you´re talking about the trailing newline only, chomp might be the ...
    (perl.beginners)