我想知道如何解析Java字符串的最后6位数字。所以:
String input1 = "b400" // the regex should return b400
String input2 = "101010" // the regex should return 101010
String input3 = "12345678" // the regex should return 345678
最佳答案
无需正则表达式。
input.substring(Math.max(0, input.length() - 6));
如果出于API原因必须是正则表达式,
Pattern.compile(".{0,6}\\Z", Pattern.DOTALL)
如果需要匹配最后6个代码点(包括补充代码点),则可以将
.
替换为(?:[\\ud800-\\udbff][\\udc00-\\udfff]|.){0,6}