㈠ 在線求助,如何利用正則表達式,過濾掉注釋代碼
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);
運行一下,你會發現郵箱地址被替換成一個鏈接了。