473,473 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Create 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 7643
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|banana).)*$/'
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******@hotmail.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.Collections.Generic;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Regex r = new Regex(".*apple|banana|cherry.*");
string[] strings =
"apple,banana,cherry,applebanana,applebananacherry ,fishapple,chips,chip
and apple,apple pie".Split(',');
foreach (string s in strings)
{
Console.WriteLine("{0} Match? {1}", s,
r.Match(s).Success);
}
Console.ReadLine();
}
}
}

You should get this:

apple Match? True
banana Match? True
cherry Match? True
applebanana Match? True
applebananacherry 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******@hotmail.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'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Oct 19 '07 #5

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

Similar topics

1
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...
4
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...
7
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...
3
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...
25
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...
11
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...
5
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 =...
3
by: Zeba | last post by:
Hi guys, I need some help regarding regular expressions. Consider the following statement : System.Text.RegularExpressions.Match match =...
4
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...
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:
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
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,...
0
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...
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,...
1
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...
0
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...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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.