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 4 3609
Hello,
Have you tried something like this
(sub:(.)*LOT:(. )+?\s)
Let me know if it solved your problem.
-------------------- From: sh*****@yahoo.c om Newsgroups: microsoft.publi c.dotnet.genera l 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************ *********@g43g2 000cwa.googlegr oups.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**********@go ogle.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**********@go ogle.com Injection-Info: g43g2000cwa.goo glegroups.com; posting-host=63.86.206. 3; posting-account=FAgaQQw AAABNdEbvXlMQ8k Q3uQ0Iry25 Path:
TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GP08.phx.gbl!ne wsfeed00.sul.t-online.de!t-onli
ne.de!news.glor b.com!postnews. google.com!g43g 2000cwa.googleg roups.com!not-fo
r-mailXref: TK2MSFTNGXA01.p hx.gbl microsoft.publi c.dotnet.genera l:188343 X-Tomcat-NG: microsoft.publi c.dotnet.genera l
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.
Unfortunately not.
It returns the same result (only one match) as my original pattern.
thanks
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
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.c om Newsgroups: microsoft.publi c.dotnet.genera l 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************ **********@g14g 2000cwa.googleg roups.com> References: <11************ *********@g43g2 000cwa.googlegr oups.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**********@go ogle.com NNTP-Posting-Date: Thu, 9 Feb 2006 16:03:41 +0000 (UTC) In-Reply-To: <11************ *********@g43g2 000cwa.googlegr oups.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**********@go ogle.com Injection-Info: g14g2000cwa.goo glegroups.com; posting-host=63.86.206. 3; posting-account=FAgaQQw AAABNdEbvXlMQ8k Q3uQ0Iry25 Path:
TK2MSFTNGXA01.p hx.gbl!TK2MSFTF EED02.phx.gbl!t ornado.fastwebn et.it!tiscali!n e
wsfeed1.ip.tisc ali.net!news.gl orb.com!postnew s.google.com!g1 4g2000cwa.googl e
groups.com!not-for-mailXref: TK2MSFTNGXA01.p hx.gbl microsoft.publi c.dotnet.genera l:188428 X-Tomcat-NG: microsoft.publi c.dotnet.genera l
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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 functions are simplified versions of the full featured methods
for compiled regular expressions. Most non-trivial applications always
use the compiled form
UNQUOTE
|
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 the T-SQL single line comment starts with a "--" and the string
character is a "'".
Considering all this, from these lines below the pattern will only catch "/*
|
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 is to improve, but not to prove.
|
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 starting and ending with a *.
I tried things like string regex = @"(^*\|*^)");
but it still doesn't work completely (matches on "*test" too for example).
If anything could help me with this, that would be appreciated.
| |
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
http://forta.com/books/0672325667/
|
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 might be a neat way to solve
this, but I am new to them. Can anyone give me a hint here?
The catch is, it must only find tokens that are not quoted and not
commented; examples follow
|
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" How should I modify
the patter to pickup only "one"?
|
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
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |