Sign in to follow this  
Followers 0
lordwud

Regular expression with an 'anything but' clause in the middle

1 post in this topic

Hey guys, I spent last night pulling my hair out trying to figure out if this is possible.

Suppose I have two strings:


balls = '\nfoo\ncat\nbar\nfoo\ndog\ncat\nbar\nfoo\ndog\nmouse\ncat\nbar\n'
tits = '\nfoo\ntiger\ncat\nlion\nbar\nfoo\ndog\nlion\nmouse\nbar\nfoo\ndog\ncat\nmouse\nbar\n'

I want to check every instance of 'foo.*?\n[\s\S\n]*?bar' and only match if the word cat is not there. So balls would fail, but tits would match.

There are a few caveats though,

It has to be one regular expression.

I do not know which regular expression engine I'm using.

There are no other scripting capabilities.

I have tried listing each character in a class with a carrot like [^c][^a][^t]

I also tried look ahead/behind/around with no success

Since I don't know which regex engine I'm using, tips in whatever you are most familiar with will be helpful.

I have access to a Safari books account, so if you can recommend a particular book, that would be good too.

variables shown easier to read:


balls:

foo
cat
bar
foo
dog
cat
bar
foo
dog
mouse
cat
bar

tits:

foo
tiger
cat
lion
bar
foo
dog
lion
mouse
bar
foo
dog
cat
mouse
bar

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0