By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,626 Members | 1,657 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,626 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.