我想将这个regexp模式2 numbers-3 numbers-5 numbers and letter分成两部分。数字和"-“数组和第二个数组中的字母。
我已经想了好一阵子了。希望我能得到一些帮助。
下面是一个例子
"12-123-12345A" <----- the string
// I want to split it such that it can be ["12-123-12345","A"]我试过这个
"\\d{2}-\\d{3}-\\d{5}"
// that only give me ["", "A"]还有这个
"(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)"
// ["12", "-", "123", "-", "12345", "A"]发布于 2014-02-27 07:20:58
\D匹配任何非数字字符(包括-)。最好使用[^-\d],而不是排除-。
String s = "12-123-12345A";
String parts[] = s.split("(?<=\\d)(?=[^-\\d])");
System.out.println(parts[0]); // 12-123-12345
System.out.println(parts[1]); // A参见演示:http://ideone.com/emr1Kq
发布于 2014-02-27 07:20:58
尝尝这个
String[] a = "12-123-12345A".split("(?<=\\d)(?=\\p{Alpha})");发布于 2014-02-27 08:19:39
(\d{2}-\d{3}-\d{5})(\w)
你可以在这个网站上测试它。
http://regexpal.com/
这是java代码。注双斜杠替换斜杠\->\
package com.company;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// http://stackoverflow.com/questions/22061614
public class Main {
public static void main(String[] args) {
Pattern regex = Pattern.compile("(\\d{2}-\\d{3}-\\d{5})(\\w)");
Matcher matcher = regex.matcher("12-123-12345A");
matcher.find();
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
// write your code here
}
}https://stackoverflow.com/questions/22061614
复制相似问题