Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.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_Regex - Fatal编程技术网

Java正则表达式,符号组合除外

Java正则表达式,符号组合除外,java,regex,Java,Regex,我试图找到子字符串包含任何字符,但不包含组合[% 例如: Input: atrololo[%trololo Output: atrololo Input: tro[tro%tro[%trololo Output: tro[tro%tro 我已经写过正则表达式了,可以使用除[或%]以外的任何符号: [A-Za-z-0-9\s!-$-&/:-@\\-`\{-~]* 我必须在表达式的末尾加上类似[^[%],但我无法解决它应该如何输入 你可以帮我登记入住 将以下内容作为测试字符串: sdf

我试图找到子字符串包含任何字符,但不包含组合[%

例如:

Input: atrololo[%trololo
Output: atrololo

Input: tro[tro%tro[%trololo
Output: tro[tro%tro
我已经写过正则表达式了,可以使用除[或%]以外的任何符号:

[A-Za-z-0-9\s!-$-&/:-@\\-`\{-~]*
我必须在表达式的末尾加上类似[^[%],但我无法解决它应该如何输入

你可以帮我登记入住

将以下内容作为测试字符串:

sdfasdsdfasa#@!55@321!2h/ хf[[[[[sds d
asgfdgsdf[[[%for (int i = 0; i < 5; i++){}%]
[% fo%][%r(int i = 0; i < 5; i++){ %]*[%}%]
[%for(int i = 0; i < 5; i++){%][%=i%][%}%]
[%@n%]<[%@ n + m %]*[%@%]>[%@%]
%?s.equals(""TEST"")%]TRUE[%@3%]![%@%][%?%]

问候。

您可以使用下面这样的基于负前瞻的正则表达式来获取[%

?:?!\[%.*匹配任何字符,但不匹配[%0]次或更多次

基于前瞻性的正则表达式

^.*?(?=\[%)

您可以根据正则表达式\[%拆分输入字符串并获得所需的部分

String s = "tro[tro%tro[%trololo";
String[] part = s.split("\\[%");
System.out.println(part[0]);  // output : tro[tro%tro

您可以使用下面这样的基于负前瞻的正则表达式来获取[%

?:?!\[%.*匹配任何字符,但不匹配[%0]次或更多次

基于前瞻性的正则表达式

^.*?(?=\[%)

您可以根据正则表达式\[%拆分输入字符串并获得所需的部分

String s = "tro[tro%tro[%trololo";
String[] part = s.split("\\[%");
System.out.println(part[0]);  // output : tro[tro%tro

将输入/输出对用作规范:

String input; // the starting string
String output = input.replaceAll("\\[%.*", "");

将输入/输出对用作规范:

String input; // the starting string
String output = input.replaceAll("\\[%.*", "");

你是天才!非常感谢!你是天才!非常感谢!这看起来像解决方案,但我无法重建输入流。你是什么意思?输入是一行字符串,而不是输入流。这看起来像解决方案,但我无法重建输入流。你是什么意思?输入是一行字符串,而不是输入流。