Spindel
Spindel

Reputation: 289

Regex match only if word count between 1-50

So I have this code:

(r'\[quote\](.+?)\[/quote\]')

What I want to do is to change the regex so it only matches if the text within [quote] [/quote] is between 1-50 words.

Is there any easy way to do this?

Edit: Removed confusing html code in the regex example. I am NOT trying to match HTML.

Upvotes: 0

Views: 394

Answers (2)

rpmartz
rpmartz

Reputation: 3809

This is a definite misuse of regexes for a lot of reasons, not the least of which is the problem matching [X]HTML as @Hyperboreus noted, but if you really insist you could do something along the lines of ([a-zA-Z0-9]\s){1}{49}.

For the record, I don't recommend this.

Upvotes: 1

Adam Smith
Adam Smith

Reputation: 54213

Sure there is, depending on how you define a "word."

I would do so separately from regex, but if you want to use regex, you could probably do:

r"\[quote\](.+?\s){1,49}[/quote\]"

That will match between 2 and 50 words (since it demands a trailing \s, it can't match ONE)

Crud, that also won't match the LAST word, so let's do this instead:

r"\[quote\](.+?(?:\s.+?){1,49})\[/quote\]"

Upvotes: 2

Related Questions