473,830 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex negative character set

Hi All,

It might be because its the end of a long week, but I can't figure out the
correct way to write a regular expression to find all matches except a
specified string.

For example, take the following 3 lines:
This is OK
This is Not OK
Again This is OK

I want to find the first and last line, and not the "This is Not OK" line.

Thanks,
PAGates
Nov 17 '05 #1
3 1822
pagates wrote:
It might be because its the end of a long week, but I can't figure out the
correct way to write a regular expression to find all matches except a
specified string.


I don't know how to write such a regex either, but I'd do it this way:

Regex r = new Regex("This is Not OK");

if (r.IsMatch(line ) == false) { // if it doesn't match "This is Not Ok"...
// ... it's got to be Ok.
}

hth,
Max

Nov 17 '05 #2
In article <78************ *************** *******@microso ft.com>,
pagates <pa*****@discus sions.microsoft .com> wrote:

: It might be because its the end of a long week, but I can't figure out
: the correct way to write a regular expression to find all matches
: except a specified string.
:
: For example, take the following 3 lines:
: This is OK
: This is Not OK
: Again This is OK
:
: I want to find the first and last line, and not the "This is Not OK"
: line.

That's actually a little tricky. If you don't believe me, try to
construct a regular expression that doesn't contain the string "ab".
Then do the same for "abc".

Why not use the much simpler approach of looking for match failures?

static void Main(string[] args)
{
string[] inputs = new string[] {
"This is OK",
"This is Not OK",
"Again This is OK"
};

Regex dontwant = new Regex(@"^This is Not OK$");

foreach (string input in inputs)
if (!dontwant.Matc h(input).Succes s)
Console.WriteLi ne(input);
}

If we had a better idea of the problem you're trying to solve, we could
probably give you more useful advice.

Greg
Nov 17 '05 #3
Hi Greg,

The problem I'm to solve is actually writing a macro in the IDE to find
(next and previous) #region keywords. I have no problem doing that, but the
unfortunate side effect is that when they are found, they are expanded.
Therefore, I was looking for a quick-n-dirty way to skip the "Component
Designer generated code" region.

The IDE Macro find allows searching using Regular Expressions, so I thought
I could do so here. More of a curiousity than something I want to spend any
significant time on.

Thanks,
PAGates

"Greg Bacon" wrote:
In article <78************ *************** *******@microso ft.com>,
pagates <pa*****@discus sions.microsoft .com> wrote:

: It might be because its the end of a long week, but I can't figure out
: the correct way to write a regular expression to find all matches
: except a specified string.
:
: For example, take the following 3 lines:
: This is OK
: This is Not OK
: Again This is OK
:
: I want to find the first and last line, and not the "This is Not OK"
: line.

That's actually a little tricky. If you don't believe me, try to
construct a regular expression that doesn't contain the string "ab".
Then do the same for "abc".

Why not use the much simpler approach of looking for match failures?

static void Main(string[] args)
{
string[] inputs = new string[] {
"This is OK",
"This is Not OK",
"Again This is OK"
};

Regex dontwant = new Regex(@"^This is Not OK$");

foreach (string input in inputs)
if (!dontwant.Matc h(input).Succes s)
Console.WriteLi ne(input);
}

If we had a better idea of the problem you're trying to solve, we could
probably give you more useful advice.

Greg

Nov 17 '05 #4

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

Similar topics

4
2149
by: aeuglein | last post by:
Hello! I have this RegEx: /(+:\/\/+)/i Now, I want to exlude on the end of a String the formats .gif / .jpg / ..png / .exe / .zip / .rar How I can this add to my regex ?
7
5734
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.
6
4804
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search string. It's actually the input string into a Microsoft Index Server search. The string will consist of words, perhaps enclosed in quotes or parentheses. I'd like to use Regex to pull out the words, or the phrases if the words are enclosed in quotes....
2
260
by: lavu | last post by:
I am trying to form a regex to search my string and give me substrings follwing the pattern that it split the string at every occurence of hyphen - character but not if a \ precedes it. ie. if a string is like this: -xyz -rer\-er -fgg12\-33 I want to get foll. substrings using the regex.split method -xyz
1
3643
by: justin.mayes | last post by:
hello all - An example. You have a regular expression to locate certain html tags. "(<)" This will find every instance of a "<" character that is not followed by a letter. The match will include the 2nd character ("<>") wheras group 1 will just include the "<" character.
11
3119
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend a hand? import regsub
6
9670
by: Gary Bond | last post by:
Hi All, Being a bit of a newbie with regex, I am confused when using word boundaries. For instance, I want to replace all the stand alone '.5k' that occur in an input string, with 500. In other words "this is a .5k example" goes to "this is a 500 example" The replace should not touch '.5k' that occurs inside a word. For example:
4
312
by: pedrito | last post by:
I have a regex question and it never occurred to me to ask here, until I saw Jesse Houwing's quick response to Phil for his Regex question. I have some filenames that I'm trying to parse out of URLs. (href=("|')http://.www\.thesite\.com/.{1,7}/)(?<filename>.) This generally works, but the problem is some of the image files have ..th.jpg at the end to indicate thumbnails. I want to exclude those. I just want the ones that don't have...
2
1529
by: Jeremy | last post by:
I am trying to replace a string "P" with "\P" as long as the string "P" does not already have a "\" in front. for my search string I've used @"(P)" so my regex replace statement is: System.Text.RegularExpressions.Regex pR = new System.Text.RegularExpressions.Regex(@"(P)"); string strResult = pR.Replace(@"This is a XP test X\P", @"\P"); The poblem is that the statement replaces XP with \P where I want it to be
3
2536
by: Smokey Grindel | last post by:
Alright so I have a string... that can be anything like this then have a number like 102.34m, yes there is a m behind it to say "this is money", no I didn't design the spec thats just how data comes in... well I had a regex that replaces all those m's in a string where there is a number before it with no m... so its just the string with 102.34... well this worked fine until we got negative money! so now when its -102.34m it still has the...
0
9777
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, 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...
0
10765
Oralloy
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...
1
10519
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,...
0
10196
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 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...
0
9307
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7737
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4408
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3954
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3070
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.