Hello Datz_cozee75,

I'd like the second word of the name to be rendered as only a first initial. I'm completely mystified by the omission of the second letter.

For the input line 2. Kim Washington, do you want the output to be Kim W or K Washington? You say “the second word of the name,” so I’m assuming the former. Here is your regex, with the captures numbered:

my $int = s/^(\d+\.)(\s+)(\w+)(\s+)(\w)(.)/$3$4$5/; # 1 2 3 4 5 6

For the given input line, captures are as follows:

^(2.)( )(Kim)( )(W)(a)shington 1 2 3 4 5 6

The substitution says: match the expression in the left-hand side (regex), then replace the matched part with the right-hand side. The former (i.e., the match) is 2. Kim Wa. The latter (i.e., the replacement) is $3$4$5, which expands to Kim W. This is replaces the matched text within the string, so 2. Kim Wa becomes Kim W and the rest of the string is unaffected. And that’s why the second letter disappears!

For this substitution, I would use a simpler regex (only one capture), like this:

s/^\d+\.\s+(\w+\s+\w).*$/$1/;

For example:

13:08 >perl -wE "my $s = '2. Kim Washington'; $s =~ s/^\d+\.\s+(\w+\s+ +\w).*$/$1/; say $s;" Kim W 13:09 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: combining lists along with a regex by Athanasius
in thread combining lists along with a regex by Aldebaran

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.