Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Larry Woods
Guest
 
Posts: n/a
#1: Jul 20 '05
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



Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#2: Jul 20 '05

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


"Larry Woods" <larry@lwoods.com> writes:
[color=blue]
> 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.[/color]

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 - lrn@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Larry Woods
Guest
 
Posts: n/a
#3: Jul 20 '05

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


Lookahead works great!
[color=blue]
> Using lookahead:
> /^((?!image).)*$/
> (probably horribly inefficient with all that lookahead)[/color]

I am using it in a vb.net application any it's "instantaneous!" (600 record
file)

Thanks.

Larry

"Lasse Reichstein Nielsen" <lrn@hotpop.com> wrote in message
news:y8x0dnjc.fsf@hotpop.com...[color=blue]
> "Larry Woods" <larry@lwoods.com> writes:
>[color=green]
> > 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"[/color][/color]
is[color=blue][color=green]
> > NOT in the string.[/color]
>
> 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 - lrn@hotpop.com
> Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
> 'Faith without judgement merely degrades the spirit divine.'[/color]


Closed Thread