Re: Matching neighbouring words of a pattern using Regex
From: Charles DeRykus (ced_at_bcstec.ca.boeing.com)
Date: 08/31/04
- Next message: nobull_at_mail.com: "Re: Perl vs Java for specific task"
- Previous message: Gunnar Hjalmarsson: "Re: Matching neighbouring words of a pattern using Regex"
- In reply to: CV: "Matching neighbouring words of a pattern using Regex"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 31 Aug 2004 00:27:35 GMT
In article <S7OdnYSjY_-DGa7cRVn-iQ@adelphia.com>,
CV <cv@nospamadelphia.net> wrote:
>How can I match 'n' number of neighbouring words of a pattern using regular
>expressions?
>
>For example, suppose I am looking for the pattern "length xyz cm" in some
>text. where xyz is a number - integer or fraction or decimal point. How can
>I also grab about 3-5 words on either side of the pattern "length xyz cm"?
>The surrounding words are not always constant & may be variable. Also, the
>original text to be matched is not just a single sentence, but lines from a
>file concatenated together - so the text has many newline characters too. I
>only want the words on the same line as the pattern.
>
>I have tried using regex of the form
>/\b(\w*)\b(\w*)\b(\w*)\b($pattern)\b(\w*)\b(\w*)\b(\w*), but this doesn't
>work for some reason. Could someone please offer some suggestions?
>
You may be confused about the \b assertion. Did you intend for
something with \w and \W..? Also, what if the pattern falls
at the beginning or end of the line... do you want to capture
the patterns that may not have 3-5 surrounding words?
One possibility presuming you intend to capture 3-5 surrounding
words:
my $text = "...";
my $pattern = 'length ... cm ';
my $words = '(?:\w+[^\w\n]+){3,5}';
#my $words = '(?:\w+[^\w\n]+){0,5}'; # to catch every pattern
print $1 while /($words$pattern$words)/g;
[ Note the 3-5 surrounding words may consume another
adjacent $pattern instance but you don't specify what
to do in that case. }
hth,
-- Charles DeRykus
- Next message: nobull_at_mail.com: "Re: Perl vs Java for specific task"
- Previous message: Gunnar Hjalmarsson: "Re: Matching neighbouring words of a pattern using Regex"
- In reply to: CV: "Matching neighbouring words of a pattern using Regex"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|