I'm looking for RegEx to return either the first [n] words in a paragraph, or if a paragraph contains less than [n] words, a full paragraph is returned.
For example, if I need, at most, the first 7 words:
<p>one two <tag>three</tag> four five, six seven eight nine ten.</p><p>ignore</p>
I would get:
one two <tag>three</tag> four five, six seven
And the same RegEx in a paragraph containing less than the requested number of words:
<p>one two <tag>three</tag> four five.</p><p>ignore</p>
Just coming back:
one two <tag>three</tag> four five.
My attempt at a problem led to the following RegEx:
^(?:\<p.*?\>)((?:\w+\b.*?){1,7}).*(?:\</p\>)
However, this returns only the first word - "one." This does not work. I think.*? (after \ w + \ b) causes problems.
Where am I mistaken? Can anyone introduce a RegEx that will work?
FYI, I am using .Net 3.5 Regex engine (via C #)
Thank you very much
source share