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.
In reply to regexp matching around a word by kidd
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |