February 6th, 2014, 03:52 PM
Adding dashes to strings
I need to convert numeric values of a certain number of characters into hyphenated versions of the same number.
For instance 98765 needs to be modified to 98-765, where 12345 would be modified to 12-345.
For the same number of characters, the dashes will always be in the same position(s), so
NNNNNNNN will always be NNN-NNN-NN
NNNNNNN will always be NNN-NN-NN
NNNNNN will always be NNN-NNN
I could hard code this (for a Java/JSF application), but I would like to be able to make this configurable in the future, hence I am looking at regex or other options.
I want to use a portable language too, since this may be used outside my JSF application in unknown languages/environments.
Any advice on how to do this with regex and/or other standard & widely available string manipulation language?
February 7th, 2014, 06:40 PM
I do not know how to do it in Java, but since most modern regex implementations derive from Perl, it might give you some help if I show you how I might do it in Perl:
Comments on this post