There is, of course, more than one way to do it, and those ways are highly dependant on what you consider to be a word. If you are content with the \w description of word, you can use the following example. You will have to modify that example if you have a more specific description of word (such as needing words to contain hyphens, apostraphes, accent characters, etc.).

You can remove the {n}th+1 word like this:

my $string = "one two three four"; $string =~ s/^((?:\w+\s+){2})(\w+)\s+\b/$1/; my $word = $2; print "$string, $word\n";

That should be enough ammunition for you to see how to put it back as well, but if not:

$string =~ s/((?:\w+\s+){2})/$1$word /;

This method has some shortcomings. One of them is that if your words are not space-delimited, you will end up with trouble. Also if your phrase of words contains punctuation, such as a comma or a period, that will also wreck havoc. But you didn't specify such details in the description of your problem. Ask the question filled with ambiguity and vagueness, you get an answer that may not solve your problem. Note that I put the space following the word back into the string. It's somewhat hard to see, but it's right after $word and before /;

A few days ago you started a similar thread, take out section of a string then put it back. You received several good answers. In this response you were advised to read the perlre perldoc page. Had you done this, you would have already known the answer to today's question. To that reading assignment I would also add perlretut and perlrequick. In fact, reverse that order, read perlrequick, perlretut, and perlre in that order. At some point through that reading the regular expression light-bulb will flick on in your mind.

I hope this advice helps ease the pain of diving into something new, and admittedly, somewhat overwhelming at first.

Dave

"If I had my life to do over again, I'd be a plumber." -- Albert Einstein


In reply to Re: Needing to take out 3rd word of sting by davido
in thread Needing to take out 3rd word of sting by mr_evans2u

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.