View Single Post
Old 01-31-2013, 09:41 PM   PM User | #15
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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..
Old Pedant is offline   Reply With Quote