Home > Net >  How would I use regular expression to match a word with at least 5 characters with 's' as
How would I use regular expression to match a word with at least 5 characters with 's' as

Time:12-04

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,}

CodePudding user response:

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

CodePudding user response:

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

  • Related