469,898 Members | 1,832 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

Regex help

ht
does anyone know a regex pattern that can match,
text within five words of text

e.g.
text hello goodbye today yesterday find text bye him her wait text

match: text bye him her wait text

I thought it would be something like text(?:[\w]*){1,5}text but that doesn't work.

I would also like to be able to change the amount of within words
Help anyone ?

thanks
TH
Jul 19 '05 #1
1 1845
In article <15**************************@posting.google.com >, ht
<to***@perfectinfo.com> wrote:
does anyone know a regex pattern that can match,
text within five words of text
It is not clear what you mean here.

e.g.
text hello goodbye today yesterday find text bye him her wait text

match: text bye him her wait text

I thought it would be something like text(?:[\w]*){1,5}text but that doesn't
work.
\w is the equivalent of a character, so it is not necessary to put it
within '[]' brackets. You probably want "one or more" characters
instead of "zero or more", so use '+' instead of '*'. Lastly, you need
to match the whitespace characters between the words:

#!/usr/local/bin/perl
use strict;
use warnings;

my $string =
"text hello goodbye today yesterday find text bye him her wait text";

if( $string =~ /text\s+(?:\w+\s+){1,5}text/ ) {
print "match\n";
}else{
print "no match\n";
}

prints "match".

I would also like to be able to change the amount of within words
I am afraid I don't know what you mean here, either.
Help anyone ?
It is best to post a complete, short-as-possible program that shows the
problem you are encountering, what the program does, and what you think
it should be doing instead.

thanks
TH


FYI: This newsgroup is defunct. Try comp.lang.perl.misc in the future
for better response.
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by jeevankodali | last post: by
17 posts views Thread by clintonG | last post: by
7 posts views Thread by Mike Labosh | last post: by
9 posts views Thread by jmchadha | last post: by
7 posts views Thread by Extremest | last post: by
6 posts views Thread by Phil Barber | last post: by
reply views Thread by Support Desk | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.