简体   繁体   English

正则表达式编号

[英]Regular expression number

My target is to format next number 01122222222 to this 011-22-22-22-22 First three numbers of number, dash and all next numbers with dash after second one. 我的目标是将下一个数字01122222222格式化为此011-22-22-22-22前三个数字,破折号和所有下一个数字,第二个后接破折号。 Already tried: 已经尝试过:

private String phoneFormat(String phoneNumber){
    String regex = "\\B(?=(\\d{3})+(?!\\d))";
    String formattedPhone = phoneNumber.replaceAll(regex, Constants.Characters.DASH);
    return formattedPhone;
}

but it produce different result. 但是会产生不同的结果。

A regex will do the trick. 正则表达式可以解决问题。 Replace sets of 2 digits with "-[digit][digit]" as long as you have 3 digits before those. 只要2位数字之前有3位数字,就可以用“-[digit] [digit]”替换两位数字。

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

Live Example 现场例子

O/P : O / P:

011-22-22-22-22

PS : This approach should NOT be used in prod environment and has been written solely to please my own stubbornness that this problem can be solved using one regex. PS:这种方法不应在产品环境中使用,其编写目的仅仅是为了使我自己own强使用此正则表达式可以解决此问题。

Since at first your question wasn't clear, I had a solution for both Java and Javascript. 由于起初您的问题尚不清楚,所以我有一个针对Java和Javascript的解决方案。 I'll leave the javascript one in here as well just 'cause :-) 我也会把javascript留在这里,只是因为:-)

Java 8 Java 8

First we use substring(0,3) to get the first three numbers. 首先,我们使用substring(0,3)来获取前三个数字。 We add - to this, and with the remaining group ( x.substring(3) ), we split them in groups of two, and we join them together with String.join and use - as the concatenating character. 我们添加- ,然后与其余的组( x.substring(3) )分成两个组,然后将它们与String.join联接在一起,并使用-作为串联字符。

String test = "01122222222";
String res = test.substring(0,3) + "-";
String[] parts = test.substring(3).split("(?=(?:..)*$)");
res += String.join("-",parts);

System.out.println(res);

Live Example 现场例子


Pre Java 8 Java 8之前的版本

From the comments it became clear that you are not using Java 8, so pre-java8 there are various other solutions. 从注释中可以明显看出您没有使用Java 8,因此在Java8之前的版本中还有其他各种解决方案。 You could use a loop like I have done, and add the last part manually. 您可以像我一样使用循环,然后手动添加最后一部分。 (Alternatively, you could just create the string with each element in the loop, and take the substring again to remove the last - ). (或者,您可以只使用循环中的每个元素创建字符串,然后再次使用子字符串以删除最后一个- )。

String test = "01122222222";
String res = test.substring(0,3) + "-";
String[] parts = test.substring(3).split("(?=(?:..)*$)");
for(int i = 0; i < parts.length-1; i++){
    res += parts[i]+"-";
}
res+=parts[parts.length-1];
System.out.println(res);

Live Example 现场例子


Javascript Java脚本

Using the same logic, you could do this in javascript as well. 使用相同的逻辑,您也可以在javascript中执行此操作。 You can run the snippet to see that the result is actually what you expected. 您可以运行该代码段以查看结果实际上是您期望的结果。

 var x = "01122222222"; var res = x.substring(0,3) +"-"+ x.substring(3).split(/(?=(?:..)*$)/).join("-"); console.log(res) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM