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

L

Larry Woods

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
 
L

Lasse Reichstein Nielsen

Larry Woods said:
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
 
L

Larry Woods

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 said:
Larry Woods said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top