Reputation: 45
I can't seem to figure out this regex https://regex101.com/r/cQ9nK8/38
These should match
This should not match
As you can see they match individually but not as a whole, I though this would work but it doesn't
(?:[[:alnum:]]+(?:\.\d+)*)*
Upvotes: 2
Views: 57
Reputation: 163362
You might use a repeating pattern:
First match [[:alpha:]]
followed by optional [[:alnum:]]
(if the first of 2 parts separated by a dot should not start with a digit)
This would match January
Followed by an optional part that matches digit
That will match January.313
Optionally repeat a dot followed by the same pattern
That will match the last 3 examples.
Pattern
^[[:alpha:]][[:alnum:]]*(?:\.[[:digit:]]+(?:\.[[:alpha:]][[:alnum:]]*\.[[:digit:]]+)*)?$
A version without the POSIX notation could be
^[a-zA-Z][a-zA-Z0-9]*(?:\.[0-9]+(?:\.[a-zA-Z][a-zA-Z0-9]*\.[0-9]+)*)?$
^
Start of string[a-zA-Z][a-zA-Z0-9]*
Match a char a-zA-Z optionally followed by a-zA-Z and digits(?:
non capture group
\.[0-9]+
Match a .
and 1+ digits(?:\.[a-zA-Z][a-zA-Z0-9]*\.[0-9]+)*
Optionally repeat matching .
a char a-zA-Z optionally followed by a-zA-Z and digits, a dot and digits)?
Close group and make it optional$
End of stringUpvotes: 2
Reputation: 75870
Here is my attempt:
^((?:^|\.)[A-Z][A-Z\d]*(?:$|\.\d+))(?1)*$
See the online demo
^
- Start string ancor.(
- Open 1st capture group/pattern.
(?:
- Open non-capture group.
^|\.
- Start string or literal dot.)
- Close non capture group.[A-Z][A-Z\d]*
- A single letter followed by zero or more letters or digits.(?:
- Open non-capture group.
$|\.
- End string or literal dot.)
- Close non capture group.
)
- Close 1st capture group/pattern.(?1)*
- Repeat the capture group/pattern zero or more times.$
- End string ancor.I've use case insensitive matching btw.
Upvotes: 2