If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

another regex question

I am slowly getting my head around regex, but really it is mostly a mystery to me.

Here's the thing: I have a string (although I have no idea how that string will look). All I know is that the string will contain a word (I don't know what that word is either). I don't know if the string will be a paragraph, a sentence or a sentence fragment (the sentence may be cut off, either at the start or the end).

But I need to get as much of the sentence containing the word as possible, without getting too much.

So I figure that these are the "rules":

- Start capturing from the closest word before the variable word that starts with a capital/uppercase.
- If there is no word that starts with a capital before the variable word, start capturing from the start of the string.
- Equally, if the part of the string after the variable word contains a full stop/period, finish capturing at the full stop.
- If not, capture until the end of the string.

I know it's not perfect logic, but it doesn't have to be - all I want to do is to be able to show the word in some sort of context, like Word does when you do spellcheck.

to start the capture at the beginning of the sentence ort the beginning of the string, instead of the end of the previous one, and to end on a full stop, exclamation, question mark or just the end of the string

Has the interesting effect of changing *which* "happy" is found in demo #5. If you really wanted the first one found, I could fix it to do that. But I'm assuming that's a case you aren't too worried about.