Erthrall
Erthrall

Reputation: 33

How would I use regular expression to match a word with at least 5 characters with 's' as the last one?

I'm trying to use regex to match: a word that's at least 5 characters long and ends with an 's', but the 's' is included in the 5 characters. Say for example, I have the following words:

hexes pixies major prairies caveman zipfiles oxes

I tried doing ([a-z]s?){5,}

Upvotes: 0

Views: 53

Answers (2)

Oskar Asplin
Oskar Asplin

Reputation: 31

To add to The fourth bird's answer: if you also want to match for capital letters, add A-Z like this:

\b[A-Za-z]{4,}s\b

You can also match for special alphabetical characters like (æøåäöüß...) with À-ȕ, like this:

\b[A-Za-zÀ-ȕ]{4,}s\b

Regex demo

Upvotes: 2

The fourth bird
The fourth bird

Reputation: 163517

The pattern ([a-z]s?){5,} repeats 5 or more times a character in the range a-z followed by an optional s char

If you only want to match characters a-z and "words" are determined by word boundaries \b, you can match 4 or more times the range a-z and end the match with an s char

\b[a-z]{4,}s\b

Regex demo

Upvotes: 2

Related Questions