Gayan Jayasingha
Gayan Jayasingha

Reputation: 802

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?

Upvotes: 0

Views: 1506

Answers (5)

lovecraftian
lovecraftian

Reputation: 71

Try using a greedy regular expression, i.e. 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.

Upvotes: 0

Pshemo
Pshemo

Reputation: 124265

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);
}

Upvotes: 0

RKC
RKC

Reputation: 1874

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.

Upvotes: 0

senseiwu
senseiwu

Reputation: 5279

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\\|", ""));
        }

Upvotes: 0

Drathier
Drathier

Reputation: 14539

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

Upvotes: 3

Related Questions