I am trying to use string split to solve my problem, but my program is only partially right. I am new to regex and still trying to understand how to implement it for complicated patterns.
我正在尝试使用字符串拆分来解决我的问题,但我的程序只是部分正确。我是regex的新手,仍然试图了解如何为复杂的模式实现它。
I have below mentioned string formats:
我有下面提到的字符串格式:
String a = Internal module control "memory" sensor
String b = Internal module control memory "sensor"
String c = "Internal" module control "memory" sensor
The output I expect should be like:
我期望的输出应该是:
String a = Internal module control "memory"/memory //(with or without quotes is fine)
String b = Internal module control memory "sensor"/sensor //(both are fine)
String c = "Internal"/Internal module control "memory"/memory //(here also both are fine)
What I tried:
我尝试了什么:
My program works perfectly fine for String a
and String c
, but when I come across case like mentioned in String b
it fails.
我的程序对String a和String c完全正常,但是当遇到String b中提到的情况时,它失败了。
String[] words = a.split("\"");
for (int i=0; i<words.length-1; i++) {
final_desc += words[i];
}
my o/p for a
, b
and c
is:
我对a,b和c的o / p是:
a = Internal module control memory
(fails) I expect: b = Internal module control memory sensor // I get b = Internal module control memory
c = Internal module control memory
P.S.
In general, I want to remove everything after last quote no matter how many quotes there are in the given string and if there are quotes in the last word then it should consider that word also, like presented in String b
一般来说,我想删除最后一个引号之后的所有内容,无论给定字符串中有多少引号,如果最后一个单词中有引号,那么它也应该考虑该单词,如字符串b中所示
2 个解决方案
#1
1
Use the other version of split
which takes an additional limit
argument.
使用另一个版本的split,它带有一个额外的限制参数。
So
replace
String[] words = b.split("\"");
String [] words = b.split(“\”“);
with
String[] words = b.split("\"", -1);
String [] words = b.split(“\”“, - 1);
reason
Here specifying a negative limit value will make split
apply the pattern as many times as possible and also not disregard the trailing empty strings. Read the Java docs for more information.
这里指定负限制值将使分割尽可能多地应用模式,也不会忽略尾随空字符串。阅读Java文档以获取更多信息。
#2
1
Try to use the replace()
method of the String.
尝试使用String的replace()方法。
Remove this block
删除此块
String[] words = a.split("\"");
for (int i=0; i<words.length-1; i++) {
final_desc += words[i];
}
and put on its place this code
并把这个代码放在它的位置
int lastQuote = a.lastIndexOf("\"");
if(lastQuote > 0) {
final_desc = a.subString(0, lastQuote).replace("\"", "");
}else {
final_desc = a;
}
#1
1
Use the other version of split
which takes an additional limit
argument.
使用另一个版本的split,它带有一个额外的限制参数。
So
replace
String[] words = b.split("\"");
String [] words = b.split(“\”“);
with
String[] words = b.split("\"", -1);
String [] words = b.split(“\”“, - 1);
reason
Here specifying a negative limit value will make split
apply the pattern as many times as possible and also not disregard the trailing empty strings. Read the Java docs for more information.
这里指定负限制值将使分割尽可能多地应用模式,也不会忽略尾随空字符串。阅读Java文档以获取更多信息。
#2
1
Try to use the replace()
method of the String.
尝试使用String的replace()方法。
Remove this block
删除此块
String[] words = a.split("\"");
for (int i=0; i<words.length-1; i++) {
final_desc += words[i];
}
and put on its place this code
并把这个代码放在它的位置
int lastQuote = a.lastIndexOf("\"");
if(lastQuote > 0) {
final_desc = a.subString(0, lastQuote).replace("\"", "");
}else {
final_desc = a;
}