I want to split this regexp pattern 2 numbers-3 numbers-5 numbers and letter
in two part. Numbers and "-" one array and the letters in the second array.
I been trying to figure it out for a while. Hoping I can get some help.
Here is an example
"12-123-12345A" <----- the string
// I want to split it such that it can be ["12-123-12345","A"]
I tried this
"\\d{2}-\\d{3}-\\d{5}"
// that only give me ["", "A"]
and this
"(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)"
// ["12", "-", "123", "-", "12345", "A"]
\\D
matches any non-digit character (including -
). You'd better to use [^-\\d]
instead to exclude -
.
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
See a demo: http://ideone.com/emr1Kq
尝试这个
String[] a = "12-123-12345A".split("(?<=\\d)(?=\\p{Alpha})");
(\\d{2}-\\d{3}-\\d{5})(\\w)
You can test it on this website
Here's the java code. note double slash in replace of slash \\ --> \\\\
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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.