Regex help

Discussion in 'Perl' started by ht, Feb 4, 2004.

  1. ht

    ht Guest

    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
    ht, Feb 4, 2004
    #1
    1. Advertising

  2. ht

    Jim Gibson Guest

    In article <>, ht
    <> 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.
    Jim Gibson, Feb 6, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    698
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,622
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    596
  4. Xah Lee
    Replies:
    1
    Views:
    939
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    754
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page