Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.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/3/android/198.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 提取特定模式后的字符串_Java_Android_Regex - Fatal编程技术网

Java 提取特定模式后的字符串

Java 提取特定模式后的字符串,java,android,regex,Java,Android,Regex,在试图理解在一个字符串中的特定模式后提取字符串的方法后,我失去了理智 模式为#|#,例如字符串为: Scirocco#01##|#CP1冷冻器 我想找到模式并在模式结束后提取字符串: CP1冷冻器 我试过使用正则表达式^(.)|##(.)$,但没有找到解决方法 String input = "Scirocco_#01#|#Cp1"; Matcher m = Pattern.compile("^(.*)#|#(.*)$").matcher(input); if(m.find()) { Stri

在试图理解在一个字符串中的特定模式后提取字符串的方法后,我失去了理智

模式为#|#,例如字符串为: Scirocco#01##|#CP1冷冻器 我想找到模式并在模式结束后提取字符串: CP1冷冻器

我试过使用正则表达式^(.)|##(.)$,但没有找到解决方法

String input = "Scirocco_#01#|#Cp1";
Matcher m = Pattern.compile("^(.*)#|#(.*)$").matcher(input);
if(m.find()) {
  String first = m.group(1); // Scirocco_#01
  String second = m.group(2); // Cp1
}

你需要逃离管道“|”

像这样:

Matcher m = Pattern.compile("^(.*)#\\|#(.*)$").matcher(input);

你需要逃离管道“|”

像这样:

Matcher m = Pattern.compile("^(.*)#\\|#(.*)$").matcher(input);

首先,您需要转义
|
,以逐字匹配它。其次,如果只希望提取
||#
后面的内容,则不需要捕获前面的内容,因此可以删除表达式的开头。第一个捕获组将包含所需的子字符串:


演示:

首先,您需要转义
|
以逐字匹配它。其次,如果只希望提取
||#
后面的内容,则不需要捕获前面的内容,因此可以删除表达式的开头。第一个捕获组将包含所需的子字符串:


演示:

Usw regex101调试此类正则表达式Usw regex101调试此类正则表达式