Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在最后一次出现集合a字符时拆分字符串?_Java_String_Split - Fatal编程技术网

Java 如何在最后一次出现集合a字符时拆分字符串?

Java 如何在最后一次出现集合a字符时拆分字符串?,java,string,split,Java,String,Split,我有一个文本文件,看起来像这样 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| 目前,我将整个内容读给一个缓冲读取器,并创建一个字符串。然后我使用split函数分割“|L |”字符上的行。这一切都很好,直到

我有一个文本文件,看起来像这样

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|
目前,我将整个内容读给一个缓冲读取器,并创建一个字符串。然后我使用split函数分割“|L |”字符上的行。这一切都很好,直到行s|u size|u desc | L | L |。这里的split函数没有按预期工作。我希望它在该行中第二次出现“|L |”时将其拆分。我该怎么做

  • 反向串
  • 第一次出现分隔符字符串时拆分
  • 反向串
  • 反向串
  • 第一次出现分隔符字符串时拆分
  • 反向串
  • 反向串
  • 第一次出现分隔符字符串时拆分
  • 反向串
  • 反向串
  • 第一次出现分隔符字符串时拆分
  • 反向串

  • 请记住,如果您排序/反转等,您必须计算这样做的成本

    但这是一种可能,在拆分后,您只需替换虚假的| L |——

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

    请记住,如果您排序/反转等,您必须计算这样做的成本

    但这是一种可能,在拆分后,您只需替换虚假的| L |——

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

    请记住,如果您排序/反转等,您必须计算这样做的成本

    但这是一种可能,在拆分后,您只需替换虚假的| L |——

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

    请记住,如果您排序/反转等,您必须计算这样做的成本

    但这是一种可能,在拆分后,您只需替换虚假的| L |——

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

    尝试使用贪婪的正则表达式,即匹配尽可能多的文本的正则表达式。例如,在扩展正则表达式中

    (L\\|)+
    

    将匹配一个或多个出现的“L |”,并尽可能多地匹配,包括问题行中的第二个“L |”。因此,在这样一个正则表达式上拆分字符串。

    尝试使用贪婪正则表达式,即匹配尽可能多的文本的正则表达式。例如,在扩展正则表达式中

    (L\\|)+
    

    将匹配一个或多个出现的“L |”,并尽可能多地匹配,包括问题行中的第二个“L |”。因此,在这样一个正则表达式上拆分字符串。

    尝试使用贪婪正则表达式,即匹配尽可能多的文本的正则表达式。例如,在扩展正则表达式中

    (L\\|)+
    

    将匹配一个或多个出现的“L |”,并尽可能多地匹配,包括问题行中的第二个“L |”。因此,在这样一个正则表达式上拆分字符串。

    尝试使用贪婪正则表达式,即匹配尽可能多的文本的正则表达式。例如,在扩展正则表达式中

    (L\\|)+
    

    将匹配一个或多个出现的“L |”,并尽可能多地匹配,包括问题行中的第二个“L |”。因此,在正则表达式上拆分字符串,如下所示。

    您可以使用正向查找来使用它,如果前面包含字符或数字,则只使用该
    |L |

    String str="Your entire string";
    str.split("(?<=\\w)\\|L\\|");
    
    String str=“您的整个字符串”;
    
    str.split((?您可以使用正面的后面查看,如果前面包含字符或数字,则只使用该
    |L |

    String str="Your entire string";
    str.split("(?<=\\w)\\|L\\|");
    
    String str=“您的整个字符串”;
    
    str.split((?您可以使用正面的后面查看,如果前面包含字符或数字,则只使用该
    |L |

    String str="Your entire string";
    str.split("(?<=\\w)\\|L\\|");
    
    String str=“您的整个字符串”;
    
    str.split((?您可以使用正面的后面查看,如果前面包含字符或数字,则只使用该
    |L |

    String str="Your entire string";
    str.split("(?<=\\w)\\|L\\|");
    
    String str=“您的整个字符串”;
    
    str.split((?假设要拆分的
    |L |
    始终位于您可以使用的行的末尾

    yourString.split("(?m)\\|L\\|$")
    
    (?m)
    是regex多行标志,它使
    ^
    $
    定位符匹配行的开始和结束(而不是整个字符串的开始和结束)


    在没有行分隔符的情况下,另一种尝试的方法是检查拆分后是否没有类似的行分隔符

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

    另一种解决方案是在从文件读取数据时,在不使用
    |L |
    的情况下创建数组

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

    假设要拆分的
    | L |
    始终位于您可以使用的行的末尾

    yourString.split("(?m)\\|L\\|$")
    
    (?m)
    是regex多行标志,它使
    ^
    $
    定位符匹配行的开始和结束(而不是整个字符串的开始和结束)


    在没有行分隔符的情况下,另一种尝试的方法是检查拆分后是否没有类似的行分隔符

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

    另一种解决方案是在从文件读取数据时,在不使用
    |L |
    的情况下创建数组

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

    假设要拆分的
    | L |
    始终位于您可以使用的行的末尾

    yourString.split("(?m)\\|L\\|$")
    
    (?m)
    是regex多行标志,它使
    ^
    $
    定位符匹配行的开始和结束(而不是整个字符串的开始和结束)


    在没有行分隔符的情况下,另一种尝试的方法是检查拆分后是否没有类似的行分隔符

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

    另一种解决方案是在从文件读取数据时,在不使用
    |L |
    的情况下创建数组

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

    假设要拆分的
    | L |
    始终位于您可以使用的行的末尾

    yourString.split("(?m)\\|L\\|$")
    
    (?m)
    是regex多行标志,它使
    ^
    $
    定位符匹配行的开始和结束(而不是整个字符串的开始和结束)


    在没有行分隔符的情况下,另一种尝试的方法是检查拆分后是否没有类似的行分隔符

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

    另一种解决方案是在从文件读取数据时,在不使用
    |L |
    的情况下创建数组

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

    为什么不在
    换行符上中断呢?这是
    “|L |”的最后一次出现