python – Is there a Regex that allows alphabets and max 3 characters (space, period, comma) after an alphabet or some alphabets?
I know someone may have tried answering this in the past, but I couldn’t find it anywhere.
I want to write a regex that must start with some alphabet/alphabets and it only allows alphabets and 3 special characters (comma, period, space).
But the catch is, it should only allow one occurrence of these characters after the alphabet/alphabets.
So for example,
- “John,. “
- “John .,”
- “John .”
- “John ,”
- “John “
- “John,. Joe, .”
These are acceptable.
But,
- “John,..” // has 2 periods
- “John.,.” // has 2 periods
- “John,.,.,. ” // has more than one occurrence of period and comma
- “,. John” // starts with a character
- “John , .” // has 2 spaces
These are not acceptable.
I’ve tried a bunch of regex expressions but none of them can ensure 1 occurence of each character after some text.
This is the regex I’ve gotten so far but it doesn’t allow “John.,” or “John,.John”
/^[a-zA-Z]+([,.\s](?![,.\s]))?[a-zA-Z\s]*$
Read more here: Source link
