英文:
Splitting a string for last occurrence a character
问题
我正在尝试使用以下格式拆分字符串:
"abc=cde,dfe=lk,f,sss=f,d,s"
我希望通过使用第一组字符作为键,第二组字符作为值,将这些值恢复到一个映射中。
例如
- 键:
abc
,值:cde
- 键:
dfe
,值:lk,f
- 键:
sss
,值:f,d,s
因此,将这些值拆分为最后一个出现的","。
有关如何执行此操作的任何想法吗?
我尝试过正则表达式和Stringtokenizer,但我无法仅恢复最后一个出现的","。
英文:
I'm trying to split a String with a format like this :
"abc=cde,dfe=lk,f,sss=f,d,s"
I'd like to recover these values in a map by using the first set of characters as a key and the second ones as value.
For example
- key:
abc
, value:cde
- key:
dfe
, value:lk,f
- key:
sss
, value:f,d,s
So splitting these values for the last occurrence of ",".
Any ideas on how to do it?
I tried with regex and Stringtokenizer but I can't manage to recover just the last occurrence of ","
答案1
得分: 1
你可以使用以下正则表达式(可能可以进行优化):
,(?=(?:(?!,).)*=)
(在Regex101上查看)
这将匹配一个逗号,
,该逗号在下一个等号=
之前没有后续的逗号,
。
英文:
You could use the following regex (could possibly be optimized):
,(?=(?:(?!,).)*=)
(see on Regex101)
This matches a ,
which has no subsequent ,
until the next =
.
答案2
得分: 0
你需要使用正则表达式来完成这个任务。
完整代码:
public class Test {
public static void main(String args[]) {
String input = "abc=cde,dfe=lk,f,sss=f,d,s";
String[] arrOfStr = input.split(",(?=(?:(?!,).)*=)");
HashMap<String, String> properties = new HashMap<String, String>();
for(int i=0;i<arrOfStr.length;i++) {
String[] temp = arrOfStr[i].split("=");
properties.put(temp[0], temp[1]);
}
System.out.println("Input String : " + input);
System.out.println("\nFinal properties : ");
properties.entrySet().forEach(entry->{
System.out.println("key = " + entry.getKey() + " :: value = " + entry.getValue());
});
}
}
输出:
Input String : abc=cde,dfe=lk,f,sss=f,d,s
Final properties :
key = dfe :: value = lk,f
key = sss :: value = f,d,s
key = abc :: value = cde
英文:
You need to use regex for this.
Full Code :
public class Test {
public static void main(String args[]) {
String input = "abc=cde,dfe=lk,f,sss=f,d,s";
String[] arrOfStr = input.split(",(?=(?:(?!,).)*=)");
HashMap<String, String> properties = new HashMap<String, String>();
for(int i=0;i<arrOfStr.length;i++) {
String[] temp = arrOfStr[i].split("=");
properties.put(temp[0], temp[1]);
}
System.out.println("Input String : " +input);
System.out.println("\nFinal properties : ");
properties.entrySet().forEach(entry->{
System.out.println("key = " +entry.getKey() + " :: value = " + entry.getValue());
});
}
}
Output :
Input String : abc=cde,dfe=lk,f,sss=f,d,s
Final properties :
key = dfe :: value = lk,f
key = sss :: value = f,d,s
key = abc :: value = cde
答案3
得分: 0
public class Test {
public static void main(String args[]) {
String text = "abc=cde,dfe=lk,f,sss=f,d,s";
String[] parts = text.split(",");
Map<String, String> map = new LinkedHashMap<>();
String key = null;
StringBuilder value = new StringBuilder();
for (int i = 0; i < parts.length; i++) {
if (parts[i].contains("=")) {
if (key != null) {
map.put(key, value.toString());
value.setLength(0);
}
String[] innerParts = parts[i].split("=");
key = innerParts[0];
value.append(innerParts[1]);
} else {
value.append(',').append(parts[i]);
}
}
map.put(key, value.toString());
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry);
}
}
}
Output:
abc=cde
dfe=lk,f
sss=f,d,s
英文:
Full Code :
public class Test {
public static void main(String args[]) {
String text = "abc=cde,dfe=lk,f,sss=f,d,s";
String[] parts = text.split(",");
Map<String, String> map = new LinkedHashMap<>();
String key = null;
StringBuilder value = new StringBuilder();
for (int i = 0; i < parts.length; i++) {
if (parts[i].contains("=")) {
if (key != null) {
map.put(key, value.toString());
value.setLength(0);
}
String[] innerParts = parts[i].split("=");
key = innerParts[0];
value.append(innerParts[1]);
} else {
value.append(',').append(parts[i]);
}
}
map.put(key, value.toString());
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(entry);
}
}
}
Output :
abc=cde
dfe=lk,f
sss=f,d,s
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论