I am trying to "prettify" a certain type of number object in my solution. This particular type has the following possible representations, along with their pretty representations:
xxxxxyyyyyyy => xxxxx-yyyyyyy
xxxxxyyyyyyyz => xxxxx-yyyyyyy-z
Basically, the type consists of 5 digits denoting FieldA
, 7 digits denoting FieldB
and an optional digit denoting a CheckDigit
. Using the following regex/replace patterns:
Regex: ^(?<FIELDA>\d{5})(?<FIELDB>\d{7})(?<CHECKDIGIT>\d?)$
Replacement: ${FIELDA}-${FIELDB}-${CHECKDIGIT}
... results in:
xxxxxyyyyyyy => xxxxx-yyyyyyy- (wrong)
xxxxxyyyyyyyz => xxxxx-yyyyyyy-z (correct)
Is it possible correcting the first representation using just a regex/replace pair? I can make this work by using two different regexes but i would like a more elegant solution than that. One that uses a single regex/replacement pair.
PS. I'm using Java 1.7.