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

How do I write a reg expression to search for NOT a substring?

I want a regular expression that will return TRUE if there is NOT a
substring in the string. Example: Return true if the substring "image" is
NOT in the string.

TIA,

Larry Woods
Jul 20 '05 #1
2 1963
"Larry Woods" <la***@lwoods.com> writes:
I want a regular expression that will return TRUE if there is NOT a
substring in the string. Example: Return true if the substring "image" is
NOT in the string.


That is hard.

It is much easier to return true if the search string is in the string
to be searched. Then use negation to return the opposite:

!(/image/).test(myStr)

If you really insist on a regular expression that matches all strings
without the substring "image", then here are some suggestions:

Using lookahead:
/^((?!image).)*$/
(probably horribly inefficient with all that lookahead)

Brute force:
/^([^i]*)((i[^im]|i$|im[^ia]|im$|ima[^ig]|ima$|imag[^ie]|imag$)[^i]*)*$/

Combination, only do lookahead on an i.
/^([^i]*)((i(?!mage))*[^i]*)*$/

Enjoy
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
Lookahead works great!
Using lookahead:
/^((?!image).)*$/
(probably horribly inefficient with all that lookahead)
I am using it in a vb.net application any it's "instantaneous!" (600 record
file)

Thanks.

Larry

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:y8**********@hotpop.com... "Larry Woods" <la***@lwoods.com> writes:
I want a regular expression that will return TRUE if there is NOT a
substring in the string. Example: Return true if the substring "image" is NOT in the string.


That is hard.

It is much easier to return true if the search string is in the string
to be searched. Then use negation to return the opposite:

!(/image/).test(myStr)

If you really insist on a regular expression that matches all strings
without the substring "image", then here are some suggestions:

Using lookahead:
/^((?!image).)*$/
(probably horribly inefficient with all that lookahead)

Brute force:
/^([^i]*)((i[^im]|i$|im[^ia]|im$|ima[^ig]|ima$|imag[^ie]|imag$)[^i]*)*$/

Combination, only do lookahead on an i.
/^([^i]*)((i(?!mage))*[^i]*)*$/

Enjoy
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #3

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

Similar topics

3
by: Ike | last post by:
Can anyone discern why the following code writes the document.write() lines literally? That is, a line like document.write('<CENTER>') should write <CENTER> but instead writes the entire ...
7
by: Scott | last post by:
I need help to modify the code below to pass url variables from a framset. The click to run this will be in the mainFrame. This script works well in a non-frame page. Grabs the current url...
6
by: dreamerbin | last post by:
Hi, I'm having trouble extracting substrings using regular expression. Here is my problem: Want to find the substring that is immediately before a given substring. For example: from "00...
8
by: Ahmad A. Rahman | last post by:
Hi all, I have a problem constructing a regular expression using .net. I have a string, separated with comma, and I want to group the string together but, I failed to group a numeric character...
10
by: Lee Kuhn | last post by:
I am trying the create a regular expression that will essentially match characters in the middle of a fixed-length string. The string may be any characters, but will always be the same length. In...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
7
by: Kevin CH | last post by:
Hi, I'm currently running into a confusion on regex and hopefully you guys can clear it up for me. Suppose I have a regular expression (0|(1(01*0)*1))* and two test strings: 110_1011101_ and...
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...
4
by: sowen | last post by:
hi, folks I am doing a automate tool to remove a project reference from the VS.NET solution file. Suppose the sln file has this project definition ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.