我想将下面的字符串分成三部分 (1)数量 (2)字符串直到第一次出现',' (3)其余的字符串
Like if the string is "12345 - electricity, flat no 1106 , Palash H , Pune"
Three parts should be
(1) 12345
(2) electricity
(3) flat no 1106 , Palash H , Pune
我可以使用下面的代码拆分成12345和其余的字符串。但不能按要求打破第2和第3部分
Map<String, String> strParts= new HashMap<String, String>();
String text = "12345 - electricity, flat no 1106 , Palash 2E , Pune";
Pattern pttrnCrs = Pattern.compile("(.*)\\s\\W\\s(.*)");
Matcher matcher = pttrnCrs.matcher(text);
if (matcher.matches()) {
strParts.put("NUM", matcher.group(1));
StrParts.put("REST", matcher.group(2));
}
任何人都可以帮忙吗?
答案 0 :(得分:1)
您需要使用具有3个捕获组的正则表达式:
^(\d+)\W*([^,]+)\h*,\h*(.*)$
在Java中使用:
final String regex = "(\\d+)\\W*([^,]+)\\h*,\\h*(.*)";
如果使用隐式锚定正则表达式的Matcher#matches()
方法,则无需在Java中使用锚点。
RegEx分手:
^ # start
(\d+) # match and group 1+ digits in group #1
\W* # match 0 or more non-word characters
([^,]+) # Match and group 1+ character that are not comma in group #2
\h*,\h* # Match comma surrounded by optional whitespaces
(.*) # match and group remaining characters in string in group #3
$ # end