我需要拆分我的正则表达式,比如“嗨,我的名字,约翰,男孩”,年龄,男性“。当我试图用它分裂成嘿嘿,我的名字,约翰,男孩,年龄,男性。但我需要输出
嘿
我的
名字
约翰,孩子
年龄
男性
line="hey,my,name,"john,boy",age,male";
String[] line1 = line.split(",")
String slNo = line1[0];
String customerId = line1[1];
String customerCategory = line1[2];
输出:嘿,我的名字,约翰,男孩,年龄,男性
发布于 2019-04-24 02:13:46
使用这个正则表达式来从find
中获取所需的字符串要比拆分简单得多,如果group1为null,那么只需要整个匹配,
"([^"]+)"|[^,]+
检查这个Java代码,
String s = "hey,my,name,\"john,boy\",age,male";
Pattern p = Pattern.compile("\"([^\"]+)\"|[^,]+");
Matcher m = p.matcher(s);
List<String> words = new ArrayList<>();
while (m.find()) {
words.add(m.group(1) == null ? m.group() : m.group(1)); // store all the found words in this ArrayList
}
String[] line1 = words.toArray(new String[words.size()]);
String slNo = line1[0];
String customerId = line1[1];
String customerCategory = line1[2];
words.forEach(System.out::println);
按你的意愿打印字符串,
hey
my
name
john,boy
age
male
https://stackoverflow.com/questions/55827558
复制