How to match a string if not a substring of another string?

I am looking for a way to match a string only if it is not a substring of another string.

Example:

I need to find if text contains string "bbbb" that is not part of a string "aaaabbbbcccc". 

The regular expression should match the lines:

 aaabbbbcccc aaaabbbbccc aaabbbbccc bbbbcccc aaaabbbb 

but does not match:

 aaaabbbbcccc 

I tried to do this using search queries:

 (?<!aaaa)bbbb(?!cccc) 

but that will not work. It only matches

 aaabbbbccc 
+4
source share
3 answers

The problem is that both statements must be true if you use it in your own way. Use alternation so that it fails only if both statements fail, but not only if only one fails.

 (?<!aaaa)bbbb|bbbb(?!cccc) 

See here at Regexr

I assume that your third example should also have four “bs”.

+2
source

if you do this:

 .{0,4}b{4}.{0,4} 

and then this:

 a{4}b{4}c{4} 
0
source
 perl -ne 'print if /bbbb/ && $_!~/aaaabbbbcccc/ ' your_input 
0
source

All Articles