kidd has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks, im in seek of your wisdom once again.
I have this string:
In this string there are several instances of the word blue. What im trying to do is fetch the text around the words "blue". So i have this regexp:my $text = "This is a test for the word blue more things blue and then + some very large text with a lot of things blue and done";
Basically what it does it finds from 0 to 5 words before the word "blue" and the same after the word "blue". This gives me the next results:my $WANT = "blue"; my @results = $text =~ m/(?:\w+\s+){0,5} (?:$WANT) (?:\s+\w+){0,5} /xgi;
But then I realized that in the first result line a word "blue" appears on the 5 words after the word "blue".a test for the word blue more things blue and then with a lot of things blue and done
So I thought that maybe I could change the regexp to find the 5 words after the word "blue" in case the word "blue" is in the words around "blue".
Maybe I could give an example, my desired result would be:
What I tried without sucess was this:a test for the word blue more things blue and then some very large with a lot of things blue and done
I hope you can understand my problem.my $WANT = "blue"; my @results = $text =~ m/(?:\w+\s+){0,5} (?:$WANT) (?:\s+\w+){0,5} (?: \s+(?:$WANT) (?:\s+\w+){0,5} )? /xgi;
Thanks in advanced for any help.
If your a spanish spoken programmer go to my site: Perl en Espaņol
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: regexp matching around a word
by dragonchild (Archbishop) on May 14, 2005 at 03:40 UTC | |
Re: regexp matching around a word
by Zaxo (Archbishop) on May 14, 2005 at 02:57 UTC | |
Re: regexp matching around a word
by saintmike (Vicar) on May 14, 2005 at 03:02 UTC | |
Re: regexp matching around a word
by sh1tn (Priest) on May 14, 2005 at 09:53 UTC |
Back to
Seekers of Perl Wisdom