bmal has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regexp Substitution Problem!!
by davorg (Chancellor) on Jan 02, 2002 at 16:34 UTC | |
|
Re: Regexp Substitution Problem!!
by simon.proctor (Vicar) on Jan 02, 2002 at 16:56 UTC | |
|
Re: Regexp Substitution Problem!!
by Trimbach (Curate) on Jan 02, 2002 at 18:07 UTC | |
|
Following: Regexp Substitution Problem!!
by bmal (Novice) on Jan 02, 2002 at 17:20 UTC |