473,480 Members | 2,019 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Matching neighbouring words of a pattern using Regex

CV
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!
Jul 19 '05 #1
2 4670
CV wrote:
How can I match 'n' number of neighbouring words of a pattern using
regular expressions?


This group is defunct. See reply in comp.lang.perl.misc.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #2
In article <S7********************@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
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3182
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images...
3
2725
by: Day Of The Eagle | last post by:
Jeff_Relf wrote: > ...yet you don't even know what RegEx is. > I'm looking at the source code for mono's Regex implementation right now. You can download that source here ( use the class...
1
6999
by: Sea Sharper | last post by:
Hi, C#, from a FileSystemWatcher I would like to catch all files with a *.* filter but then inside the event handler compare against a list of wildcards (eg '*.abc;*.def') Is there anywhere...
1
1905
by: Iwan Petrow | last post by:
Hi, I want to count words in a string which are NOT with length 10. For me word is a sequence of symbols different from ' '. So I want to match these words. I think that the pattern has to be...
1
23763
by: Codex Twin | last post by:
Hello folks I am trying to use regular expressions to achieve the following: If my searchString is: "London Trains" Paris Amsterdam "Berlin Cars" I want to be able to split this string by...
5
5729
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
7
3256
by: Kevin CH | last post by:
Hi, I'm currently running into a confusion on regex and hopefully you guys can clear it up for me. Suppose I have a regular expression (0|(1(01*0)*1))* and two test strings: 110_1011101_ and...
11
4801
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
1
3410
by: Joe Strout | last post by:
Wow, this was harder than I thought (at least for a rusty Pythoneer like myself). Here's my stab at an implementation. Remember, the goal is to add a "match" method to Template which works like...
0
7041
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7081
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6737
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5336
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.