简体   繁体   中英

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|" characters. This is all good until the line 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|" in that line. How can I do this?

  1. Reverse string
  2. Split on first occurrence of your delimiter string
  3. Reverse string

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| 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|" 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| if it contains a character or number before,

String str="Your entire string";
str.split("(?<=\\w)\\|L\\|");

This should work.

Assuming that |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).


In case there are no lines separator other way to try would be checking if after |L| you split on there is no L| like

yourString.split("\\|L\\|(?!L\\|)")

Another solution would be creating your array without |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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM