[英]How to split a string on last occurrence of set a characters?
I have a text file which looks like this, 我有一个看起来像这样的文本文件,
sku_id||01276116147620|L|
s_code||01276116|L|
s_prnt_prd||147620|L|
s_clr_fmly||White|L|
s_disp_clr||White|L|
s_size_desc||L|L|
s_status_str||Clearance|L|
s_ftr_cd||0|L|
Currently I read the whole thing to a buffered reader and create a single string. 目前,我将整个内容读到缓冲的读取器中并创建一个字符串。 Then I use the split function to split the line on the "|L|"
然后,我使用split函数在“ | L |”上分割行 characters.
字符。 This is all good until the line s_size_desc||L|L|.
直到行s_size_desc ||| L | L |为止,一切都很好。 Here the split function doesn't work as expected.
在这里,分割功能无法正常工作。 I want it to split it on the second occurrence of "|L|"
我希望它在第二次出现“ | L |”时将其拆分 in that line.
在那条线。 How can I do this?
我怎样才能做到这一点?
Remember if you sort/reverse etc. you have to count the cost of doing so. 请记住,如果您进行排序/反转等,则必须计算这样做的成本。
But this is one possibility where you just replace the spurious |L| 但这是仅替换伪造的| L |的一种可能性。 after splitting -
分裂后-
String st = "sku_id||01276116147620|L|s_code||01276116|L|s_prnt_prd||147620|L|s_clr_fmly||White|L|s_disp_clr||White|L|s_size_desc||L|L|s_status_str||Clearance|L|s_ftr_cd||0|L|";
for(String ss : st.split("\\|L\\|")) {
System.out.println(ss.replaceAll("L\\|", ""));
}
Try using a greedy regular expression, ie one that will match as much text as possible. 尝试使用贪婪的正则表达式,即一个将匹配尽可能多的文本的表达式。 For example, in Extended Regular Expressions,
例如,在扩展正则表达式中,
(L\\|)+
will match one or more occurrences of "L|", and will match as many as possible, including the second "L|" 将匹配一个或多个出现的“ L |”,并且将匹配尽可能多的匹配项,包括第二个“ L |” in your problematic line.
在您的问题线上。 So split your string on a regular expression like this.
因此,将字符串拆分成这样的正则表达式。
you can use this using a positive look behind which only use that |L|
您可以使用正面的外观使用它,而仅使用
|L|
if it contains a character or number before, 如果它之前包含一个字符或数字,
String str="Your entire string";
str.split("(?<=\\w)\\|L\\|");
This should work. 这应该工作。
Assuming that |L|
假设
|L|
you want to split on is always at the end of line you can use 您想要分割的位置总是可以使用的行尾
yourString.split("(?m)\\|L\\|$")
(?m)
is regex multiline flag and it makes ^
and $
anchors match start and end of lines (instead of start and end of entire string). (?m)
是正则表达式多行标志,它使^
和$
锚匹配行的开头和结尾(而不是整个字符串的开头和结尾)。
In case there are no lines separator other way to try would be checking if after |L|
如果没有行分隔符,另一种尝试的方法是检查
|L|
之后是否 you split on there is no L|
你分裂没有
L|
like 喜欢
yourString.split("\\|L\\|(?!L\\|)")
Another solution would be creating your array without |L|
另一个解决方案是创建不带
|L|
数组|L|
while reading your data from file. 同时从文件读取数据。
Scanner scanner = new Scanner(new File(yourFile));
while(scanner.hasNextLine()){
String line = scanner.nextLine();
int lastIndex = line.lastIndexOf("|L|");
String lineWithoutL = line.substring(0,lastIndex);//do what you want with it
System.out.println(lineWithoutL);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.