Ne7WoRK
Ne7WoRK

Reputation: 161

Regular expression for 8 to 10 letter words

I need a regular expression that matches either 8 letter words ending in "tion" or 10 letter words ending in "able".

Here is what I came up with, but for some reason http://regex101.com tells me there are no matches when I try to match a string.

My idea is as follows:

([a-z]{4}^\btion\b|[a-z]{6}^\bable\b)

Link to regex101 - Here

Upvotes: 2

Views: 1242

Answers (2)

mrzasa
mrzasa

Reputation: 23317

Try this one:

\b([a-z]{4}tion|[a-z]{6}able)\b

Demo

You use ^\b between the variable section (e.g. [a-z]{4}) and constant postfix (e.g. tion) and that breaks the match. ^ means "beginning of the string (or a line)" and \b means "word boundary". Using it together makes little sense, as beginning of the string is always a word boundary.

Upvotes: 3

Barmar
Barmar

Reputation: 780843

\b matches a word boundary. You should only have this at the beginning and end of the word, not before the suffix. You can take it outside the grouping parentheses, since all the alternatives are supposed to match at word boundaries.

\b([a-z]{4}tion|[a-z]{6}able)\b

You don't need ^ at all, it matches the beginning of the string.

Upvotes: 4

Related Questions