GafferG
GafferG

Reputation: 1043

regex (preg_replace) match word except where it starts a sentence/paragraph

Im currently scratching my head to figure out a way to match a word except where it starts a sentence or paragraph.

I have a large amount of text where i need to lowercase the start of a set word, apart from if it starts a sentence

so far i have

preg_replace('/Word\b/','word',$text);

Can someone please help with the exclusion pattern of starting a sentence

thanks

Upvotes: 0

Views: 220

Answers (1)

cbreezier
cbreezier

Reputation: 1314

This really depends on how you define a sentence.

preg_replace('/([^.?!]\s*)Word\b/','$1word',$text);

This ensures that the word does not immediately follow some punctuation. It also means that it can't match the start of a paragraph because it needs to match some non-punctuation character before it.

Upvotes: 1

Related Questions