Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello Monks, im in seek of your wisdom once again.

I have this string:

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";
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 $WANT = "blue"; my @results = $text =~ m/(?:\w+\s+){0,5} (?:$WANT) (?:\s+\w+){0,5} /xgi;
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:
a test for the word blue more things blue and then with a lot of things blue and done
But then I realized that in the first result line a word "blue" appears on the 5 words after the word "blue".

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:

a test for the word blue more things blue and then some very large with a lot of things blue and done
What I tried without sucess was this:
my $WANT = "blue"; my @results = $text =~ m/(?:\w+\s+){0,5} (?:$WANT) (?:\s+\w+){0,5} (?: \s+(?:$WANT) (?:\s+\w+){0,5} )? /xgi;
I hope you can understand my problem.

Thanks in advanced for any help.

If your a spanish spoken programmer go to my site: Perl en Espaņol

In reply to regexp matching around a word by kidd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (10)
As of 2024-04-18 12:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found