473,408 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

HowTo? RegEx - pattern to exclude the whole word

I am trying to extract the pattern like this :

"SUB: some text LOT: one-word"

Described, "SUB" and "LOT" are key words; I want those words,
everything in between and one word following the "LOT:". Source text
may contain multiple "SUB: ... LOT:" blocks.

For example this is my source text:

SUB: this text I want to extract LOT: 2345 , something in between, new
SUB: again something I want to extract LOT: 2145 and more text here,
the end

When I apply this pattern:

SUB:\s+[^\r\n]+\s+LOT:\s+[^\r\n\s]+

in .NET's Regex.Matches(...), I only get one match:

SUB: this text I want to extract LOT: 2345 , something in between, new
SUB: again something I want to extract LOT: 2145

Obviously, something in this regex tells it to be "greedy", and I need
the partial matches too.

I thought this pattern would return ALL matches, which are:
1) SUB: this text I want to extract LOT: 2345
2) SUB: again something I want to extract LOT: 2145
3) SUB: this text I want to extract LOT: 2345 , something in between,
new SUB: again something I want to extract LOT: 2145

The last one I don't need of course, but I can handle it - ignore it,
and use only the first two.

So my idea was to modify my pattern to read like this:
give me all matches resembling text between "SUB:" and "LOT:",
including those keywords, plus one word after "LOT:", but (!) the text
between cannot contain "LOT:"

If I manage to compose such RegEx pattern, it would even eliminate the
result 3), and return only what I really need. But the problem is how
to define pattern that will eliminate (exclude) the whole word. I
tried "[^ ... ]" pattern, but that works only for single characters
listed between the brackets.
For example:

SUB:\s+[^\r\n(LOT:)]+\s+LOT:\s+[^\r\n\s]+

is not working. I thought that "( )" brackets would group the
characters and tell the regex not the match the appearance of the whole
word "LOT:". But instead, it invalidates any text that contain any of
these characters:
) ( : L T O

So if you could answer at least one of the following questions, I would
appreciate it very much:

1) generally, how do you compose the regex pattern to not match the
text that contain certain word?
2) if there is no easy solution for 1), or there is a better solution
for the problem I described above, what is it?

Thank you so much!

Shone

Feb 8 '06 #1
4 3571
Hello,

Have you tried something like this

(sub:(.)*LOT:(.)+?\s)

Let me know if it solved your problem.

--------------------
From: sh*****@yahoo.com
Newsgroups: microsoft.public.dotnet.general
Subject: HowTo? RegEx - pattern to exclude the whole word
Date: 8 Feb 2006 11:01:28 -0800
Organization: http://groups.google.com
Lines: 67
Message-ID: <11*********************@g43g2000cwa.googlegroups. com>
NNTP-Posting-Host: 63.86.206.3
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
X-Trace: posting.google.com 1139425293 27748 127.0.0.1 (8 Feb 2006 19:01:33 GMT)X-Complaints-To: gr**********@google.com
NNTP-Posting-Date: Wed, 8 Feb 2006 19:01:33 +0000 (UTC)
User-Agent: G2/0.2
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; NET CLR 1.1.4322),gzip(gfe),gzip(gfe)Complaints-To: gr**********@google.com
Injection-Info: g43g2000cwa.googlegroups.com; posting-host=63.86.206.3;
posting-account=FAgaQQwAAABNdEbvXlMQ8kQ3uQ0Iry25
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfee d00.sul.t-online.de!t-onli
ne.de!news.glorb.com!postnews.google.com!g43g2000c wa.googlegroups.com!not-fo
r-mailXref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.general:188343
X-Tomcat-NG: microsoft.public.dotnet.general

I am trying to extract the pattern like this :

"SUB: some text LOT: one-word"

Described, "SUB" and "LOT" are key words; I want those words,
everything in between and one word following the "LOT:". Source text
may contain multiple "SUB: ... LOT:" blocks.

For example this is my source text:

SUB: this text I want to extract LOT: 2345 , something in between, new
SUB: again something I want to extract LOT: 2145 and more text here,
the end

When I apply this pattern:

SUB:\s+[^\r\n]+\s+LOT:\s+[^\r\n\s]+

in .NET's Regex.Matches(...), I only get one match:

SUB: this text I want to extract LOT: 2345 , something in between, new
SUB: again something I want to extract LOT: 2145

Obviously, something in this regex tells it to be "greedy", and I need
the partial matches too.

I thought this pattern would return ALL matches, which are:
1) SUB: this text I want to extract LOT: 2345
2) SUB: again something I want to extract LOT: 2145
3) SUB: this text I want to extract LOT: 2345 , something in between,
new SUB: again something I want to extract LOT: 2145

The last one I don't need of course, but I can handle it - ignore it,
and use only the first two.

So my idea was to modify my pattern to read like this:
give me all matches resembling text between "SUB:" and "LOT:",
including those keywords, plus one word after "LOT:", but (!) the text
between cannot contain "LOT:"

If I manage to compose such RegEx pattern, it would even eliminate the
result 3), and return only what I really need. But the problem is how
to define pattern that will eliminate (exclude) the whole word. I
tried "[^ ... ]" pattern, but that works only for single characters
listed between the brackets.
For example:

SUB:\s+[^\r\n(LOT:)]+\s+LOT:\s+[^\r\n\s]+

is not working. I thought that "( )" brackets would group the
characters and tell the regex not the match the appearance of the whole
word "LOT:". But instead, it invalidates any text that contain any of
these characters:
) ( : L T O

So if you could answer at least one of the following questions, I would
appreciate it very much:

1) generally, how do you compose the regex pattern to not match the
text that contain certain word?
2) if there is no easy solution for 1), or there is a better solution
for the problem I described above, what is it?

Thank you so much!

Shone


--

Thank You,
Nanda Lella,

This Posting is provided "AS IS" with no warranties, and confers no rights.

Feb 8 '06 #2
Unfortunately not.
It returns the same result (only one match) as my original pattern.

thanks

Feb 8 '06 #3
Foud it!
Actually it's more a work-around than a solution, but as long as it
works...

The approach: natural ingenuity and ancient wisdom: "if you can't beat
them - join them!".
So RegEx doesn't have required pattern, i.e. [^...] pattern requires
single character. OK, I'll give it a single character. Simply replace
all occurences of the keyword "LOT:" in the source string with one
single character. Only have to be careful to pick the one that
certainly will not appear as a regular character contained in original
text. In this case ~ (tilda) is fine. So the regex pattern is:

SUB:\s+[^\r\n~]+\s+~\s+[^\r\n\s]+

Applied to modified source text it returns me exactly what I want.

Thanks for looking, take care...
Shone

Feb 9 '06 #4
Glad to see that you figured it out.

and; by the way (SUB:(.)*LOT:(.)+?\s) works for me.
I dont know how you are implementing it. I used case insensitive and
multiline options. And it returned follwing results.

+ Match [0] SUB: this text I want to extract LOT: 2345
+ Match [1] SUB: again something I want to extract LOT: 2145
--------------------
From: sh*****@yahoo.com
Newsgroups: microsoft.public.dotnet.general
Subject: Re: HowTo? RegEx - pattern to exclude the whole word
Date: 9 Feb 2006 08:03:36 -0800
Organization: http://groups.google.com
Lines: 20
Message-ID: <11**********************@g14g2000cwa.googlegroups .com>
References: <11*********************@g43g2000cwa.googlegroups. com>
NNTP-Posting-Host: 63.86.206.3
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
X-Trace: posting.google.com 1139501021 6216 127.0.0.1 (9 Feb 2006 16:03:41 GMT)X-Complaints-To: gr**********@google.com
NNTP-Posting-Date: Thu, 9 Feb 2006 16:03:41 +0000 (UTC)
In-Reply-To: <11*********************@g43g2000cwa.googlegroups. com>
User-Agent: G2/0.2
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; NET CLR 1.1.4322),gzip(gfe),gzip(gfe)Complaints-To: gr**********@google.com
Injection-Info: g14g2000cwa.googlegroups.com; posting-host=63.86.206.3;
posting-account=FAgaQQwAAABNdEbvXlMQ8kQ3uQ0Iry25
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTFEED02.phx.gbl!tornad o.fastwebnet.it!tiscali!ne
wsfeed1.ip.tiscali.net!news.glorb.com!postnews.goo gle.com!g14g2000cwa.google
groups.com!not-for-mailXref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.general:188428
X-Tomcat-NG: microsoft.public.dotnet.general

Foud it!
Actually it's more a work-around than a solution, but as long as it
works...

The approach: natural ingenuity and ancient wisdom: "if you can't beat
them - join them!".
So RegEx doesn't have required pattern, i.e. [^...] pattern requires
single character. OK, I'll give it a single character. Simply replace
all occurences of the keyword "LOT:" in the source string with one
single character. Only have to be careful to pick the one that
certainly will not appear as a regular character contained in original
text. In this case ~ (tilda) is fine. So the regex pattern is:

SUB:\s+[^\r\n~]+\s+~\s+[^\r\n\s]+

Applied to modified source text it returns me exactly what I want.

Thanks for looking, take care...
Shone


--

Thank You,
Nanda Lella,

This Posting is provided "AS IS" with no warranties, and confers no rights.

Feb 9 '06 #5

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

Similar topics

4
by: Seb | last post by:
Hi, Has anyone an idee how i can replace every character in a string if it is not alphanumeric ? something like eregi_replace, but i don't know how i say in regex NOT. Tnx
75
by: Xah Lee | last post by:
http://python.org/doc/2.4.1/lib/module-re.html http://python.org/doc/2.4.1/lib/node114.html --------- QUOTE The module defines several functions, constants, and an exception. Some of the...
5
by: Ali Eghtebas | last post by:
Hi, I've made this regex to catch the start of a valid multiline comment such as "/*" in e.g. T-SQL code. "(?<=^(?:*'*')*?*)(?<!^(?:*'*')*?--.*)/\*.*?$" With Multiline option on. As we know...
7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
7
by: Razzie | last post by:
Hey all, Decided to give a shot at Regular expressions - need a bit of help :) I can't seem to find the right regex for matching words like "*test*" or *somevalue*" - in short, all words...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
6
by: Lubomir | last post by:
Hi, I am using the following pattern: "\\b" + MySttring + "\\b" If MyString is "one", this should pick up whole words like "one". The problem is, it will pick up also the word: "one.two"...
8
by: james_027 | last post by:
hi, how do I regex that could check on any of the value that match any one of these ... 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec' Thanks james
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.