Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.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/17.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/2/ionic-framework/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正则表达式,用于在每2个字母之间匹配具有可选多个注释的单词(如何反向引用正则表达式子表达式)_Java_Regex_Backreference - Fatal编程技术网

Java正则表达式,用于在每2个字母之间匹配具有可选多个注释的单词(如何反向引用正则表达式子表达式)

Java正则表达式,用于在每2个字母之间匹配具有可选多个注释的单词(如何反向引用正则表达式子表达式),java,regex,backreference,Java,Regex,Backreference,我需要一个java正则表达式来匹配一个单词,并考虑到每两个后续字母中可能包含一条注释。 例如,“W/*comment1*/或/*comment2*/D”。 我尝试使用命名的捕获组和反向引用: (?<comment>\s*/\*.*\*/\s*)W\k<comment>*O\k<comment>*R\k<comment>*D 这是可行的,但是有没有更优雅的解决方案,而不需要多次重复“注释”子模式?您可以通过一次捕获一个(或多个)字母,同时丢弃以下可

我需要一个java正则表达式来匹配一个单词,并考虑到每两个后续字母中可能包含一条注释。 例如,
“W/*comment1*/或/*comment2*/D”
。 我尝试使用命名的捕获组和反向引用:

(?<comment>\s*/\*.*\*/\s*)W\k<comment>*O\k<comment>*R\k<comment>*D

这是可行的,但是有没有更优雅的解决方案,而不需要多次重复“注释”子模式?

您可以通过一次捕获一个(或多个)字母,同时丢弃以下可选注释来实现这一点,如下所示:

        String toBeParsed="W/* this is comment 1 */OR/*this is comment 2*/D";
        String regexp = "(\\w+)(/\\*.*?\\*/)*"; // match letters + optional comment
        Pattern pattern =Pattern.compile(regexp);
        Matcher matcher=pattern.matcher(toBeParsed);
        String word="";
        while(matcher.find()){
            String letter=matcher.group(1);
            String comment=matcher.group(2);
            System.out.println("found letter(s) "+letter);
            word+=letter;
            if (comment!=null) System.out.println("discarding comment "+matcher.group(2));
        }
        System.out.println(word);
输出是

found letter(s) W
discarding comment /* this is comment 1 */
found letter(s) OR
discarding comment /*this is comment 2*/
found letter(s) D
WORD
“如何反向引用正则表达式子表达式”

你是说这个

"(.*)\\1"

这匹配任何重复的单词\1表示第一组,即第一个括号内的部分。

输入输出示例可能会有所帮助。首先剥离注释如何?我认为这是一条路要走。它使代码变得更简单,运行两个简单的正则表达式可能比运行一个复杂的正则表达式要快得多。另外,它更容易维护和填充…@vks这是一个例子:W/*comment1*/或/*comment2*/DYeah和现在的输出?关于或甚至可能是用短语:
“(\W+)(/\\\*[a-zA-Z0-9\s!:?\*]*?\\*/)*”
。OP说的是“单词”,所以我假设它们只表示字母字符,但我怀疑这是在使用非字母字符。另外,OP没有说评论中没有空格,所以我更改了评论的限定词。是的,用短语是一个更好的主意。我将相应地修改我的解决方案
"(.*)\\1"