Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.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/regex/20.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,周围也有类似的例子,但我无法找到我的解决方案 目标:删除/*和*/之间的所有(包括)数据。我正在使用下面的代码 String str="this /* asdasda */ is test"; str = str.replaceAll("/\\*.*?\\*/","") ; System.out.println(str); 输出: this is test 当这些是嵌套的时,问题就来了 String str="this /* asdas/* extra comments */da */ i

周围也有类似的例子,但我无法找到我的解决方案

目标:删除/*和*/之间的所有(包括)数据。我正在使用下面的代码

String str="this /* asdasda */ is test";
str = str.replaceAll("/\\*.*?\\*/","") ;
System.out.println(str);
输出:

this  is test
当这些是嵌套的时,问题就来了

String str="this /* asdas/* extra comments */da */ is test";
str = str.replaceAll("/\\*.*?\\*/","") ;
System.out.println(str);
输出

this da */ is test
我无法求解嵌套零件。我需要它来丢弃/*和之间的任何内容*/

比如说

"this /* asdas/* extra commen/*   asdas  */  ts */da */ is test /* asdas */ asdasd ";
应该迟到吗

this  is test  asdasd 

replaceAll
中的正则表达式更改为
/\\*.\\*/

运行:

String str="this /* asdas/* extra comments */da */ is test";
str = str.replaceAll("/\\*.*\\*/","") ;
System.out.println(str);
String str="hello /*/* asdas/* eer/* hemmllo*/ */da */ */world";
str = str.replaceAll("/\\*.*\\*/","") ;
System.out.println(str);
输出:

this  is test
hello world
this  is test  asdasd 
this   is   test
运行:

String str="this /* asdas/* extra comments */da */ is test";
str = str.replaceAll("/\\*.*\\*/","") ;
System.out.println(str);
String str="hello /*/* asdas/* eer/* hemmllo*/ */da */ */world";
str = str.replaceAll("/\\*.*\\*/","") ;
System.out.println(str);
输出:

this  is test
hello world
this  is test  asdasd 
this   is   test
编辑: 假设您使用简单的单词,请尝试以下方法:

replaceAll("(/\\*( *(\\w) *)*)|(( *(\\w) *)*\\*/)","").replaceAll("\\*/|/\\*","");
测试1:

输出:

   here  here to
测试2:

输出:

this  is test
hello world
this  is test  asdasd 
this   is   test
测试3:

输出:

this  is test
hello world
this  is test  asdasd 
this   is   test

Java不支持递归匹配,因此您无法一次性完成所有匹配。对于支持递归正则表达式匹配的语言,可以尝试以下操作:

(\/\*(?>[^\/\*\*\/]+\;(?1))*\*/)

要在Java中模拟递归匹配,您可以在实际正则表达式之外添加逻辑(进行多次传递)

取出
|(?1)
零件,您就有了

(\/\*(?>[^\/\*\*\*/]+)*\*/)

如果您使用这样的模式匹配器:

    String regex = "(\\/\\*(?>[^\\/\\*\\*\\/]+)*\\*\\/)";
    String str="this /* asdas/* extra commen/*   asdas  */  ts */da */ is test /* asdas */ asdasd ";;

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(str);
    while (m.find()) {
        str = m.replaceFirst("");
        m = p.matcher(str);
        System.out.println(str);
    }

    // here your string is 
    // this  is test  asdasd 
最后一关将为您提供所需的字符串


请参阅联机试用正则表达式,我发现它很有用。

在嵌套的情况下,预期的输出是什么?@ofig:same。修改了QT您所使用的字符只有英文字母和“*”/”?,或者您不能确定?只有英文字母和/*和*/张贴在问题中,因为无法格式化评论感谢您在这方面的帮助。删除/*和/1之间的所有字符后。Test1/huhu这里到2。测试2这是测试asdasd 3。Test3这个test我认为正则表达式对我没有帮助,我必须为它编写separte逻辑来遍历字符串