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 用于检测具有双斜杠(/)的行(\n)结尾的正则表达式_Java_Regex - Fatal编程技术网

Java 用于检测具有双斜杠(/)的行(\n)结尾的正则表达式

Java 用于检测具有双斜杠(/)的行(\n)结尾的正则表达式,java,regex,Java,Regex,对于此示例,我需要一个正则表达式: //This is a comment and I need this \n position String notwanted ="//I do not need this end of line position"; 这应该对你有用。真的很糟糕。真的想不出更好的、多功能的解决方案。我想你也希望得到这样的评论: String myStr = "asasdasd"; //some comment here 试试这个正则表达式: (?<

对于此示例,我需要一个正则表达式:

    //This is a comment and I need this \n position
    String notwanted ="//I do not need this end of line position";

这应该对你有用。真的很糟糕。真的想不出更好的、多功能的解决方案。我想你也希望得到这样的评论:

String myStr = "asasdasd"; //some comment here

试试这个正则表达式:

(?<!")\/\/[^\n]+(\n)
但是,使用以下方法就足够了:

(?<!")\/\/[^\n]+

(?是否要匹配该换行符?您是否有任何尝试?您想对该换行符做什么?我正在对代码进行重塑,因此我需要适当的位置来打破它。我只检测到注释,但不知道如何包含\n尝试
”(?它就像一个符咒。如果可能的话,我需要不匹配
“String notwanted=\”//我不//需要这个//行尾位置\”;";
aswell@m.cekiera非常感谢。上面给出的示例很有效。我手上还有最后一个问题。我知道我的示例可能非常模糊,对此我深表歉意。如果我在新行中添加另一条注释,如
//这是另一条新注释。它没有捕获该行的位置。感谢您抽出时间我再次为您所经历的不便表示歉意。@Sean发生这种情况可能是因为此新注释的末尾没有
\n
字符。因此,请尝试将正则表达式末尾的
\n
替换为
$
(行尾元字符),如:
(?@m.cekiera我尝试在它后面添加\n,但不起作用,这就是我发布通知的原因。我还尝试用$替换\n,但这次我没有捕获任何内容。捕获此内容时还有另一个问题:String myStr=“asasdasd”;//我需要这个位置well@Sean因此,您想获得每个有效Java注释的结束位置吗?这与您提出的问题不同。您如何处理它?请在您的问题中添加代码片段,或者添加一些更详细的信息,因为很难确定什么是错误的,因为对我来说,它工作正常谢谢,但是我不需要匹配
“String notwanted=\”//我不//需要这个//行尾位置\“;”;
以及“@iismathwizard”非常感谢。上面给出的示例很有效。我手上还有最后一个问题。我知道我的示例可能非常模糊,对此我深表歉意。如果我在新行中添加另一条注释,如
//这是另一条新注释。它没有捕捉到该行的位置。感谢您抽出时间,我再次道歉感谢您所经历的不便。
public class Main {
    public static void main(String[] args){
        String example = "//This is a comment and I need this \\n position\n" +
                "String notwanted =\"//I do not need this end of line position\";";
        Pattern regex = Pattern.compile("(?<!\")//[^\\n]+(\\n)");
        Matcher matcher = regex.matcher(example);
        while (matcher.find()) {
                System.out.println(matcher.start(1));
        }
    }
}
(?<!")\/\/[^\n]+
example.split("(?<=^//[^\n]{0,1000})\n");
(?m)(?<=^//[^\n]{0,1000})\n
public class Main {
    public static void main(String[] args){
        String example =
                "//This is a comment and I need this \\n position\n" +
                "String notwanted =\"//I do not need this end of line position\";\n" +
                "String a = aaa; //comment here";
        Pattern regex = Pattern.compile("(?m)(?<=(^|;\\s{0,1000})//[^\n]{0,1000})(\n|$)");
        Matcher matcher = regex.matcher(example);
        while(matcher.find()){
            System.out.println(matcher.start());
        }
        System.out.println(example.replaceAll("(?<=(^|;\\s{0,1000})//[^\n]{0,1000})(\n|$)", " (X)\n"));
    }
}