第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從java中的源代碼中刪除注釋

從java中的源代碼中刪除注釋

Qyouu 2023-03-09 16:50:49
我想從 java 源代碼文件中刪除所有類型的注釋語句。例子:    String str1 = "SUM 10"      /*This is a Comments */ ;       String str2 = "SUM 10";     //This is a Comments"      String str3 = "http://google.com";   /*This is a Comments*/    String str4 = "('file:///xghsghsh.html/')";  //Comments    String str5 = "{\"temperature\": {\"type\"}}";  //comments預期輸出:    String str1 = "SUM 10";     String str2 = "SUM 10";      String str3 = "http://google.com";    String str4 = "('file:///xghsghsh.html/')";    String str5 = "{\"temperature\": {\"type\"}}";我正在使用下面的正則表達式來實現(xiàn):    System.out.println(str1.replaceAll("[^:]//.*|/\\\\*((?!=*/)(?s:.))+\\\\*/", ""));這給了我 str4 和 str5 的錯誤結果。請幫我解決這個問題。使用 Andreas 解決方案:        final String regex = "//.*|/\\*(?s:.*?)\\*/|(\"(?:(?<!\\\\)(?:\\\\\\\\)*\\\\\"|[^\\r\\n\"])*\")";        final String string = "    String str1 = \"SUM 10\"      /*This is a Comments */ ;   \n"             + "    String str2 = \"SUM 10\";     //This is a Comments\"  \n"             + "    String str3 = \"http://google.com\";   /*This is a Comments*/\n"             + "    String str4 = \"('file:///xghsghsh.html/')\";  //Comments\n"             + "    String str5 = \"{\"temperature\": {\"type\"}}";  //comments";        final String subst = "$1";        // The substituted value will be contained in the result variable        final String result = string.replaceAll(regex,subst);        System.out.println("Substitution result: " + result);它的工作除了 str5。
查看完整描述

4 回答

?
交互式愛情

TA貢獻1712條經(jīng)驗 獲得超3個贊

要使其工作,您需要“跳過”字符串文字。您可以通過匹配字符串文字、捕獲它們以便保留它們來做到這一點。


以下正則表達式將執(zhí)行此操作,用作$1替換字符串:


//.*|/\*(?s:.*?)\*/|("(?:(?<!\\)(?:\\\\)*\\"|[^\r\n"])*")


有關演示,請參見regex101 。


Java代碼是:


str1.replaceAll("//.*|/\\*(?s:.*?)\\*/|(\"(?:(?<!\\\\)(?:\\\\\\\\)*\\\\\"|[^\r\n\"])*\")", "$1")

解釋


//.*                      Match // and rest of line

|                        or

/\*(?s:.*?)\*/            Match /* and */, with any characters in-between, incl. linebreaks

|                        or

("                        Start capture group and match "

  (?:                      Start repeating group:

     (?<!\\)(?:\\\\)*\\"     Match escaped " optionally prefixed by escaped \'s

     |                      or

     [^\r\n"]                Match any character except " and linebreak

  )*                       End of repeating group

")                        Match terminating ", and end of capture group

$1                        Keep captured string literal


查看完整回答
反對 回復 2023-03-09
?
紫衣仙女

TA貢獻1839條經(jīng)驗 獲得超15個贊

我推薦一個兩步過程;一個基于行尾 (//),另一個不基于行尾 (/* */)。

我喜歡帕維爾的想法;但是,我看不到它如何檢查以確保星號是斜線后的下一個字符,反之亦然。

我喜歡安德烈亞斯的想法;但是,我無法讓它處理多行注釋。

https://docs.oracle.com/javase/specs/jls/se12/html/jls-3.html#jls-CommentTail


查看完整回答
反對 回復 2023-03-09
?
守著一只汪

TA貢獻1872條經(jīng)驗 獲得超4個贊

正如其他人所說,正則表達式在這里不是一個好的選擇。您可以使用簡單的DFA來完成此任務。

這是一個示例,它將為您提供多行注釋 ( /* */) 的間隔。

您可以對單行注釋 ( // -- \n) 執(zhí)行相同的方法。


    String input = ...; //here's your input String


    //0 - source code, 

    //1 - multiple lines comment (start) (/ char)

    //2 - multiple lines comment (start) (* char)

    //3 - multiple lines comment (finish) (* char)

    //4 - multiple lines comment (finish) (/ char)

    byte state = 0; 

    int startPos = -1;

    int endPos = -1;

    for (int i = 0; i < input.length(); i++) {

        switch (state) {

        case 0:

            if (input.charAt(i) == '/') {

                   state = 1;

                   startPos = i;

            }

            break;

        case 1:

            if (input.charAt(i) == '*') {

                state = 2;

            }

            break;

        case 2:

            if (input.charAt(i) == '*') {

               state = 3;

            }

            break;

        case 3:

            if (input.charAt(i) == '/') {

                state = 0;

                endPos = i+1;


                //here you have the comment between startPos and endPos indices,

                //you can do whatever you want with it

            }


            break;

        default:

            break;

        }

    }


查看完整回答
反對 回復 2023-03-09
?
寶慕林4294392

TA貢獻2021條經(jīng)驗 獲得超8個贊

也許,最好從多個簡單的表達式開始,逐步進行,例如:

.*(\s*\/\*.*|\s*\/\/.*)

最初刪除內聯(lián)評論。

演示

測試

import java.util.regex.Matcher;

import java.util.regex.Pattern;


final String regex = "(.*)(\\s*\\/\\*.*|\\s*\\/\\/.*)";

final String string = "    String str1 = \"SUM 10\"      /*This is a Comments */ ;   \n"

     + "    String str2 = \"SUM 10\";     //This is a Comments\"  \n"

     + "    String str3 = \"http://google.com\";   /*This is a Comments*/\n"

     + "    String str4 = \"('file:///xghsghsh.html/')\";  //Comments\n"

     + "    String str5 = \"{\\\"temperature\\\": {\\\"type\\\"}}\";  //comments";

final String subst = "\\1";


final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);

final Matcher matcher = pattern.matcher(string);


// The substituted value will be contained in the result variable

final String result = matcher.replaceAll(subst);


System.out.println("Substitution result: " + result);


查看完整回答
反對 回復 2023-03-09
  • 4 回答
  • 0 關注
  • 290 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號