導航:首頁 > 凈水問答 > 過濾注釋正則表達式

過濾注釋正則表達式

發布時間:2021-12-10 12:58:23

㈠ 在線求助,如何利用正則表達式,過濾掉注釋代碼

java 不熟悉。 用 javascript 的話這樣 var sHtml = '如何用正則表達式' + '這里還有一些內容' + '' + '' + ''; sHtml = sHtml.replace(//gmi, ''); alert(sHtml); java似乎可以這樣: pattern = Pattern.compile(""); matcher = pattern.matche...

㈡ 用正則表達式在java怎麼去匹配注釋 //的單行注釋 /*單行注釋*/ /* *多行注釋 */

先說結果:
我是測試出來了正則表達式,但是是針對三種注釋,寫了三種正則。
沒有實現單一的正則表達式,支持所有的注釋類型的。
代碼你可以參考參考:

//單行注釋
String commentsStr = "//this is single line comments";
Pattern singleLineCommentP = Pattern.compile("^//.*?$");
Matcher foundSingleLineComment = singleLineCommentP.matcher(commentsStr);
boolean foundSingle = foundSingleLineComment.matches();
System.out.println(foundSingleLineComment);
System.out.println(foundSingle);

/*單行注釋*/
String doubleStarCommentStr = "/* this is double star comments */";
Pattern doubleStarCommentP = Pattern.compile("^/\\*.*?\\*/$");
Matcher foundDoubleStarComment = doubleStarCommentP.matcher(doubleStarCommentStr);
boolean foundDouble = foundSingleLineComment.matches();
System.out.println(foundDoubleStarComment);
System.out.println(foundDouble);

/*
*多行注釋
*/

String multiLineComments = "/* \n" +
"* this is \n" +
"* multi line comment \n" +
"*/";
Pattern multiLineCommentP = Pattern.compile("^/\\*.*\\*/$", Pattern.DOTALL);
Matcher foundMultiLineComment = multiLineCommentP.matcher(multiLineComments);
boolean foundMulti = foundMultiLineComment.matches();
System.out.println(foundMultiLineComment);
System.out.println(foundMulti);

感興趣的話,可以去看看我總結的:
crifan 正則表達式學習心得

(此處不給貼地址,請用google搜標題,即可找到帖子地址)

㈢ java中 用正則表達式想去掉一個字元串中的注釋,怎麼弄

你在Java中要轉義某個字元需要用兩個轉義符才行.

應該這么寫

str=str.replaceAll("/\*.*\*/","");

System.out.println(str);

㈣ 正則表達式過濾中文

/^(^([\\u4E00-\\u9FA5]|[\\uFE30-\\uFFA0]))*$/

你是要這個吧? ^在正則表達式中,還有字元串開始的意思....

㈤ 設計正則表達式 要求匹配被注釋掉的行

$pattern="#^(s*//.*?)$#m";//php語言的正則,用多行模式,你沒說明語言,自己參考語法改一個

㈥ 如何用正則表達式 過濾 特定內容

正則表達式:^\d+(\.\d+)?$
你可以用這個正則表達式匹配輸入的字元,如果不匹配說明是非法的字母和字元.

㈦ 求一正則表達式,或其他什麼辦法,把字元串中的注釋去掉

用Java正則表達式替換就行了.
完整的程序如下:
class tempt
{
public static void main(String[] args)
{
String s="<?xml version=\"1.0\" encoding=\"GB18030\"?><!--XML信息頭,必填--><UFTP><MsgHdrRq><RefId>10318</RefId><!--系統參考號--><TrnCode>1602</TrnCode><!--交易代碼-->";
System.out.println(s.replaceAll("\\<\\!\\-\\-.*?\\-\\-\\>", ""));
}
}
運行結果:
<?xml version="1.0" encoding="GB18030"?><UFTP><MsgHdrRq><RefId>10318</RefId><TrnCode>1602</TrnCode>

㈧ 如何正則表達式去掉代碼中的注釋

/*[sS]**/|//.*

將符合以上正則內容替換為空

㈨ 正則表達式過濾html標簽和標簽中的注釋

|using System;
// 不過仔細看,我這個也沒錯啊...
// MyRule=@"<(?:[^><]|版""[^""]""|'[^']')*>";
// 實際檢驗一下:權

using System.Text.RegularExpressions;

class Test
{
static void Main()
{
string s = @"<td onmouseover=""if (a > 0)"">abc</td>def";
string r = @"<(?:[^><]|""[^""]""|'[^']')*>";
string t = Regex.Replace(s, r, "");
Console.WriteLine(t); // 輸出:「 0)">abcdef」
}
}

㈩ 正則表達式注釋

當正則表達式過於復雜時,可以像普通的程序語言那樣為正則表達式添加註釋,以方便閱讀。當設定了「x」修正符後,字元組(即][]內的內容)之外的所有空白字元會被忽略,#號和換行符之間的內容會被視為注釋。多說無益,舉個例子:

$text = '[email protected]';
$reg = '{
\b
#把捕獲的地址保存到$1
(
\w[-.\w]* #username
@
[-\w]+(\.[-\w]+)*\.(com|e|info) #hostname
)
\b
}ix';
//$reg = '/\b(\w[-.\w]*@[-\w]+(\.[-\w]+)*\.(com|e|info))\b/ix';
$text = preg_replace($reg,'<a href="mailto:$1">$1</a>',$text);
var_mp($text);

運行一下,你會發現郵箱地址被替換成一個鏈接了。

閱讀全文

與過濾注釋正則表達式相關的資料

熱點內容
金昌養殖污水處理設備有哪些 瀏覽:300
酚醛樹脂的性能特點 瀏覽:646
反滲透預是用什麼做成的 瀏覽:563
冷凝爐磁性過濾器 瀏覽:447
德國企業工業廢水處理 瀏覽:872
納濾凈水器推薦 瀏覽:847
純凈水桶上的三角形怎麼剪 瀏覽:488
凈化器買多少錢的 瀏覽:159
康佳凈水器最好的多少錢 瀏覽:598
海爾空氣凈化器wifi怎麼開 瀏覽:83
漢斯頓的RO膜是干什麼用的 瀏覽:667
凈水器不出靜水是什麼原因 瀏覽:584
vrv室內機有帶提升泵的嗎 瀏覽:707
凈水器過濾完100多 瀏覽:193
gl8的機油濾芯跟什麼車的通用 瀏覽:948
酚醛環氧樹脂f51增韌 瀏覽:967
ro膜可以用酒精有清洗 瀏覽:624
洗網板廢水處理 瀏覽:421
濾芯混合料孔隙率為什麼不能過小 瀏覽:550
工業污水處理膜分離設備 瀏覽:601