Regex help

H

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
 
J

Jim Gibson

ht said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top