Reputation: 543
I am trying to build a regular expression which goal is to follow 3 rules:
(1) If the input is alphanumeric and it could be split into two groups containing letters and numbers, then letters are prefix and numbers are suffix. For example:
input: ABC123
output: prefix = ABC, suffix = 123
(2) If the input is numeric only, then there is no prefix, only suffix. For example:
input: 123
output: prefix = '', suffix = 123
(3) If the input is mixed alphanumeric, then all numbers to each other at the end of input are suffix and all other characters are prefix
input: A9B9C123
output: prefix = A9B9C, suffix = 123
I have tried this so far:
"(?P<prefix>\D*)(?P<suffix>\d*)"
This matches rules (1) and (2) but NOT (3).
"(?P<prefix>.*)(?P<suffix>[0-9]+)"
This matches rule (3), but breaks rules (1) and (2).
Note: Input always ends with a number and prefix is optional.
Thank you for your help.
Upvotes: 0
Views: 4003
Reputation: 174696
Anchors are must important here. ^.*?
will do a non-greedy match of zero or more characters until (?P<number>[0-9]+)
the last number. And the last number is captured by the second group.
^(?P<prefix>.*?)(?P<number>[0-9]+)$
Upvotes: 1