| | |
Regexp
Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2006
Posts: 3
Reputation:
Solved Threads: 0
Hi,
I'm currently writing a routine to extract one, two and three word phrases from a string but with two or three word phrases I'm not getting all the phrases. For example
the string "blah blah blah" will show one ocurance of "blah blah" when really there is two.
My code is
while ($str =~ m/(\w+) (\w+)/g)
{
}
Any help would be appricated\
Thanks
Bruce
I'm currently writing a routine to extract one, two and three word phrases from a string but with two or three word phrases I'm not getting all the phrases. For example
the string "blah blah blah" will show one ocurance of "blah blah" when really there is two.
My code is
while ($str =~ m/(\w+) (\w+)/g)
{
}
Any help would be appricated\
Thanks
Bruce
•
•
Join Date: Jan 2006
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Rashakil Fol
That's not your code; it doesn't output anything at all.
While doing a global search, Perl continues just after the end of the previous match.
The full code is
while ($str =~ m/(\w+) (\w+)/g)
{
$keywords{'I_'.$1.'_'.$2}{'Cnt'} += 2;
$keywords{'I_'.$1.'_'.$2}{'Word'} = "$1 $2";
}
My problem is that perl continues after the end of the last match so with a string of "This is a test" I'll get
"this is" and "a test" but I won't get "is a" even though it's a valiv phrase.
Thanks
Bruce
Then don't do it that way. Match only single words, and take the array of single words and work with that. If you're worried about in-between characters, and only want a single space between, do another match for contiguous strings of non-word characters, and then you're set to write some code that ties things together.
All my posts may be redistributed under the GNU Free Documentation License.
•
•
Join Date: Jan 2006
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Rashakil Fol
Then don't do it that way. Match only single words, and take the array of single words and work with that. If you're worried about in-between characters, and only want a single space between, do another match for contiguous strings of non-word characters, and then you're set to write some code that ties things together.
I'd expect something simple like this could be done easily with regexp. I was hoping there was a flag or something that I didn't know about so it could look at more than the last match.
Oh well back to the hard way :rolleyes:
Thanks
Bruce
![]() |
Similar Threads
- Regexp? (C)
- vBulletin mod_rewrite (PHP)
- VERY URGENT MATLAB HELP for regexp (Computer Science)
- RegExp.replace Not Working (JavaScript / DHTML / AJAX)
- regExp pattern (Java)
- Is there a program that creates DFA from expression (Computer Science)
- Need help getting specific info out of a file (*nix Software)
Other Threads in the Perl Forum
- Previous Thread: I get the error no file or directory when i try to run a perl script
- Next Thread: PDF images
| Thread Tools | Search this Thread |
Tag cloud for Perl






