I think I have an idea of how to accomplish what you're looking for. Since your code doesn't make clear what your exact need is I'll just do an example from scratch, and you can adapt it easily to work within the framework of your code. I'm making the assumption that your first word will always match \w+. If it won't, you can switch ^\w+\b with ^.+?\b .....I think. ;)

Here's the example:

my $string = "Here is our test string."; my $word; ( $word, $string ) = split /^(\w+)\b/, $string, 2;

Now you have $word containing the first word found in $string, assuming a word, to you, contains nothing but word characters. And $string now contains only everything after the first word (including the leading space).

To put the word back, just concatenate:

$string = $word . $string;

This could also be done with a regular plain vanilla pattern match in several ways, but I'll do it the easy way:

my $string = "This is the test string."; my $word; ( $word, $string ) = $string =~ m/^(\w+)\b(.*)/;

As you can see above, we don't really care about greediness of .* because we want it to be greedy enough to grab everything following the first word. If you have newlines within the string you'll have to use the /s modifier.

Here's another approach with substitution:

my $string = "Here is our test string."; my $word = $string =~ s/^(\w+)\b(.*)/$2/;

Now you've pushed the first word into $word, and captured everything else in $2, which then becomes the substitute for the original string. Again, we don't mind of .* is greedy as long as it leaves enough for a word at the beginning of the string followed by a word boundry. Again, if there is a newline in the string, you'll need the /s modifier on the regexp.

With all of the examples above, you put humpty dumpty back together again by concatenating:

$string = $word . $string;

I hope this helps!

Dave

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


In reply to Re: take out section of a string then put it back by davido
in thread take out section of a string then put it back 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.