473,725 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular expression to match only strings NOT containing particular words

I can write a regular expression that will only match strings that are
NOT the word apple:

^([^a].*|a[^p].*|ap[^p].*|app[^l].*|apple.+)$

But is there a neater way, and how would I do it to match strings that
are NOT the word apple OR banana? Then what would be needed to match
only strings that do not CONTAIN the word "apple" or "banana" or
"cherry"?

I'd love it if the following worked:

^[^(apple)(banana )(cherry)]*$

But it appears the parantheses are ignored, as

^[(apple)(banana) (cherry)]*$

simply matches any string that consists entire of the characters
a,b,c,e,h,l,n,r ,p & y.

Oct 19 '07 #1
4 7667
Dylan Nicholson wrote:
I can write a regular expression that will only match strings that are
NOT the word apple:

^([^a].*|a[^p].*|ap[^p].*|app[^l].*|apple.+)$

But is there a neater way, and how would I do it to match strings that
are NOT the word apple OR banana? Then what would be needed to match
only strings that do not CONTAIN the word "apple" or "banana" or
"cherry"?
!(/apple/ or /banana/ or /cherry/)

jue
Oct 19 '07 #2
2007-10-18, 22:00(-07), Dylan Nicholson:
I can write a regular expression that will only match strings that are
NOT the word apple:

^([^a].*|a[^p].*|ap[^p].*|app[^l].*|apple.+)$

But is there a neater way, and how would I do it to match strings that
are NOT the word apple OR banana? Then what would be needed to match
only strings that do not CONTAIN the word "apple" or "banana" or
"cherry"?

I'd love it if the following worked:

^[^(apple)(banana )(cherry)]*$

But it appears the parantheses are ignored, as

^[(apple)(banana) (cherry)]*$

simply matches any string that consists entire of the characters
a,b,c,e,h,l,n,r ,p & y.
With perl regexps:

perl -ne 'print if /^(?:(?!apple|ba nana).)*$/'
or probably better:
perl -ne 'print if /^(?!.*(?:apple| banana))/'

But then, why not

perl -ne 'print if !/apple|banana/'

Note that vim's regexps have an equivalent negative look-ahead
operator.

--
Stéphane
Oct 19 '07 #3
On Thu, 18 Oct 2007 22:00:28 -0700, Dylan Nicholson
<wi******@hotma il.comwrote:
>I can write a regular expression that will only match strings that are
NOT the word apple:

^([^a].*|a[^p].*|ap[^p].*|app[^l].*|apple.+)$

But is there a neater way, and how would I do it to match strings that
are NOT the word apple OR banana? Then what would be needed to match
only strings that do not CONTAIN the word "apple" or "banana" or
"cherry"?

I'd love it if the following worked:

^[^(apple)(banana )(cherry)]*$

But it appears the parantheses are ignored, as

^[(apple)(banana) (cherry)]*$

simply matches any string that consists entire of the characters
a,b,c,e,h,l,n, r,p & y.
A simple way is to write the regex to match apple or banana or cherry,
do the match and then check the Success property of the match object.

Execute the following mini program

using System;
using System.Collecti ons.Generic;
using System.Text.Reg ularExpressions ;

namespace ConsoleApplicat ion1
{
class Program
{
static void Main(string[] args)
{
Regex r = new Regex(".*apple| banana|cherry.* ");
string[] strings =
"apple,banana,c herry,applebana na,applebananac herry,fishapple ,chips,chip
and apple,apple pie".Split(',') ;
foreach (string s in strings)
{
Console.WriteLi ne("{0} Match? {1}", s,
r.Match(s).Succ ess);
}
Console.ReadLin e();
}
}
}

You should get this:

apple Match? True
banana Match? True
cherry Match? True
applebanana Match? True
applebananacher ry Match? True
fishapple Match? True
chips Match? False
chip and apple Match? True
apple pie Match? True

--
http://bytes.thinkersroom.com
Oct 19 '07 #4
On Thu, 18 Oct 2007 22:00:28 -0700, Dylan Nicholson
<wi******@hotma il.comwrote:
>But is there a neater way, and how would I do it to match strings that
are NOT the word apple OR banana? Then what would be needed to match
only strings that do not CONTAIN the word "apple" or "banana" or
"cherry"?
The general answer is that you should use separate regexen and logical
operators, or an explicit !~ but the subject of negating regexen is
discussed to some depth in the following thread @ PM:

http://perlmonks.org/?node_id=588315
Michele
--
{$_=pack'B8'x25 ,unpack'A8'x32, $a^=sub{pop^pop }->(map substr
(($a||=join'',m ap--$|x$_,(unpack'w ',unpack'u','G^ <R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX 0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,red o}#JAPH,
Oct 19 '07 #5

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

Similar topics

1
4177
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
4
3225
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go over each document, find out if it contains a header and/or a footer and extract only the main content part. The headers and the footers have no specific format and I have to detect and remove them using a list of strings that may appear as...
7
3828
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way to avoid multiple copies of a string. Any help will be highly appreciated
3
2564
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular expression. ^(.+?) uses (?!a spoon)\.$
25
5161
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
11
3108
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
5
2317
by: Avi Kak | last post by:
Folks, Does regular expression processing in Python allow for executable code to be embedded inside a regular expression? For example, in Perl the following two statements $regex = qr/hello(?{print "saw hello\n"})mello(?{print "saw mello\n"})/; "jellohellomello" =~ /$regex/;
3
2752
by: Zeba | last post by:
Hi guys, I need some help regarding regular expressions. Consider the following statement : System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(requestPath, "(*?\ \.ashx)"); (where requestPath is a string)
4
1631
by: carlos | last post by:
I am working on a regular expression validation for my search page. What I have so far works for most cases, but I would like to fine tune it some. I am new to regular expressions, and I do not have the time to read up some more on it. Can someone help? What I would like to do is allow words to be parsed using quotes. However, they can also include boolean searching. Lastly, I need to ensure the character's do not exceed a certain...
0
8888
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
8752
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,...
0
9401
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
9176
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
9113
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...
1
6702
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...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.