java - Regular expression number -


my target format next number 01122222222 011-22-22-22-22 first 3 numbers of number, dash , next numbers dash after second one. tried:

private string phoneformat(string phonenumber){     string regex = "\\b(?=(\\d{3})+(?!\\d))";     string formattedphone = phonenumber.replaceall(regex, constants.characters.dash);     return formattedphone; } 

but produce different result.

a regex trick. replace sets of 2 digits "-[digit][digit]" long have 3 digits before those.

public static void main(string[] args) {     string s = "01122222222";     system.out.println(s.replaceall("(?<=\\d{3})(\\d{2})+?", "-$1")); } 

live example

o/p :

011-22-22-22-22 

ps : approach should not used in prod environment , has been written solely please own stubbornness problem can solved using 1 regex.


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -