有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

“^^^”上的正则表达式Java拆分字符串

我希望有人能帮我设计一种模式,严格按照三个^的字符序列分割字符串,即^^^

Input: Sample-1^^^Sample-2
Output: String 1: Sample-1 and String-2: Sample-2

我试过\\^\\^\\^,它对快乐之路很有效。但如果我给它一个字符串,比如:

Input: Sample-1^^^^Sample-2

我得到的输出是:

String 1: Sample-1
String-2: ^Sample-2

我也尝试了这个模式,但没有成功


共 (1) 个答案

  1. # 1 楼答案

    在这种情况下,您需要匹配one or more文字^字符的\^+^{}):

    String[] output = input.split("\\^+");
    

    或者,如果只想匹配3 or 4文字^字符,可以使用:

    String[] output = input.split("\\^{3,4}");
    

    或者,如果要匹配3 or more文字^字符,可以使用:

    String[] output = input.split("\\^{3,}");