Matching neighbouring words of a pattern using Regex

From: CV (cv_at_nospamadelphia.net)
Date: 08/30/04


Date: Mon, 30 Aug 2004 15:45:58 -0400

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?

thanks!



Relevant Pages

  • Re: [SLE] Postfix - Sandy - 1 more Q re: /etc/postfix/recepient_check
    ... I'm a little hesitant to answer, because I'm not sure what "regular" means in this case. ... Regular expressions are an absolute must for adminstrators. ... You will encounter these pattern matching expressions in most scripting and programming languages. ... Additionally it will use the content in the round brackets as $1, $2 (content of first found expression in brackets, second found... ...
    (SuSE)
  • Re: [SLE] Postfix - Sandy - 1 more Q re: /etc/postfix/recepient_check
    ... I'm a little hesitant to answer, because I'm not sure what "regular" means in this case. ... Regular expressions are an absolute must for adminstrators. ... You will encounter these pattern matching expressions in most scripting and programming languages. ... Additionally it will use the content in the round brackets as $1, $2 (content of first found expression in brackets, second found... ...
    (SuSE)
  • Re: Help with Regex and trying to mimic the VB "like" comparison
    ... But I don't want to have to teach hundreds of users how to use regular ... expressions, as this is far to complex for their needs. ... > You can use regexes, just allow the pattern the user types be the pattern for the regex, but with a few precautions: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: searching for a pattern for multiple matches
    ... abc 234.45.68.91 xyz ... pattern found? ... Now read both perlre and perlretut. ...
    (comp.lang.perl.misc)
  • Re: grep command or operator
    ... expressions. ... I though that 'e' in egrep was just random letter put there. ... egrep - search a file for a pattern using full regular ...
    (comp.unix.solaris)