For the following string:
‘cat sheep horse cat tac dog’
I would like to write a regular expression that matches any substring
that is prefixed by the word ‘cat’, is then followed by any characters
as long as those characters do not comprise the word ‘cat’, and then
finally suffixed by the string ‘dog’. Therefore, this expression
should match the substring ‘cat tac dog’ in the above string.
Obviously, if I write an expression like:
irb(main):002:0> /cat.*dog/.match(‘cat sheep horse cat tac dog’).to_s
=> “cat sheep horse cat tac dog”
it will match the entire string.
And the non-greedy Kleene doesn’t buy me anything either since the
expression matches the first cat found anyway:
irb(main):003:0> /cat.*?dog/.match(‘cat sheep horse cat tac dog’).to_s
=> “cat sheep horse cat tac dog”
What I think I want to do is to negate a sequence of characters,
rather than just a character class, but I have looked around and not
found anything quite right.
Of course, there are ways of hacking this out, e.g. I could reverse
the string first and match ‘god’ followed by the first instance of
‘tac’, but I am hoping there is a more elegant way to do this with a
single regular expression.
Thanks–