㈠ 在线求助,如何利用正则表达式,过滤掉注释代码
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);
运行一下,你会发现邮箱地址被替换成一个链接了。