473,287 Members | 1,463 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,287 software developers and data experts.

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 7630
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
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

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.