|
Andrew: I'm pretty sure this is wrong:
(?:^|\.|\?\!)
The ^ character only means negation when used inside of [ ].
In any case, you forgot the | between \? and \! if you were looking for "or" conditions. And also, in any case, you are missing parens.
But I'm pretty sure that should be
(?:[^\.\?\!])
But I think that
(?!(\.|\?|\!))
would also work. ?! is a *negative* non-capture. The ! is the negation character for captures, not the ^
Did you test it? Against many samples, as I did?
*********
EDIT: I did test it.
I tested both your version:
/(?:^|\.|\?|\!)?[\w]([^.]*happy[^.]*)(?:\.|\?|\!|$)/
(I added the missing | before the first \!)
And my modification:
/(?:[^\.\?\!])?[\w]([^.]*happy[^.]*)(?:([\.|\?|\!]|$))/;
Neither passed all tests.
Neither could find "happy" in aardvarks whistle. happy dogs bark
Neither isolated the sentence in either
aardvarks whistle dixie! happy dogs bark
or
happy happy happy! and even more happy?
(that is, in both cases they returned the entire test string)
I will say that your (?:^|\.|\?|\!) seemed to have mostly worked. Surprised me.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Last edited by Old Pedant; 01-31-2013 at 09:56 PM..
|