Hi, I have a string ($sentence) containing pattern in this format:

A~~~.*~A

Now I wanna extract all "A~~~.*~A" from the string, each time i find one match, I change the $_ to contain the rest of the string, then sub the string by s/$_// and assign to $remainPart, so that the $remainPart only store the portion before this "A~~~.*~A" pattern..

However, i have a problem here, i extract $_ from $sentence, but cannot use it to sub $remainPart(which is equal to $sentence originally). Wondering why. Any suggestion?

Here is the source code:

#! usr/bin/perl $sentence = " he PP study VB A~~~language~A NP and CC play VB A~~~game~A NP ." $_ = $sentence; while ($_ ne '') { if (m/A~~~(.*)~A/g) { # global match $remainPart = $sentence; ($_) = /(\G(.|\n)*)/g; # store rest of the string # (after pos) to $_ $remainPart =~ s/$_//; print "$remainPart\n"; } else { $_ = ''; } }

By right, first time the $_ will contain

" NP and CC play VB A~~~game~A NP ."

but as the $remainPart is printed out, it is still same as $sentence, which indicates the substitution is not successful.


In reply to Regexp Substitution Problem!! by bmal

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.