1

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.

AweSIM
  • 1,651
  • 4
  • 18
  • 37
  • The fastest way -> Should be `^(?\d{5})(?\d{7})(?\d)?$` then in a callback check if group 3 matched. Otherwise, try to replace individually if Java supports the `\G` construct. –  Feb 17 '16 at 03:55
  • im not sure i follow completely.. care to elaborate a bit more mate? – AweSIM Feb 17 '16 at 04:22

1 Answers1

0

I don't think this is possible in a single RegEx in the tool. You can use two RegEx as follow.

First, replace the numbers with - in between.

Search: ^(\d{5})(\d{7})(\d+)?$

Replace: $1-$2-$3

First Replacement

Then, remove the trailing dash.

Search: ^(\d{5})-(\d{7})-$

Replace: $1-$2

Second Replacement

End Result

Community
  • 1
  • 1
Tushar
  • 85,780
  • 21
  • 159
  • 179
  • yea thats a possible solution as well.. however, im interested in knowing whether there is a solution that involves only a SINGLE pair of regex/replacement strings.. =/ – AweSIM Feb 17 '16 at 03:23