String testStr="\t\t\fwwqf\"wefeasfdf"+property+"asfdsfd\"asfd";
这是一个.java文件里的一行代码
现在用java读取文件时,要判断这行代码中哪些是字符串,把是字符串的两头分别加上<#>、</#>
要求输出String testStr=<#>"\t\t\fwwqf\"wefeasfdf"</#>+property+<#>"asfdsfd\"asfd"</#>;
这是一个.java文件里的一行代码
现在用java读取文件时,要判断这行代码中哪些是字符串,把是字符串的两头分别加上<#>、</#>
要求输出String testStr=<#>"\t\t\fwwqf\"wefeasfdf"</#>+property+<#>"asfdsfd\"asfd"</#>;
所以要查找文件中是字符串的字符串
http://www.java2html.com/如果是在网页上想显示高亮的代码,可以使用 JavaScript 处理,有一个工具:http://code.google.com/p/syntaxhighlighter/
public static void main(String[] args) {
Matcher m=KEYPT.matcher(str);
while(m.find()){
System.out.println(m.group());
}
}
给个正则式吧
要求输出
"\t\t\fwwqf\"wefeasfdf"
"asfdsfd\"asfd"
String str = "/**"; String str2 = "*/";这些你的程序会处理成什么样子的?我只能提供一些正则表达式专写的东西,可能对你有用(正则表达式语法,在 Java 中使用时需要转义):匹配多行注释:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/
匹配单行注释://[^\n]*
匹配双引号:"(?:\\.|[^\\"])*"你做的这些东西我原来用 JavaScript 曾经做过,但是有很多很多的 bug,我已经是没什么精力去改了,
附上一下作为参考吧。<script type='text/javascript'>
var Syntax = { // Java code with syntax higher light,
// and add line number.
replace : function(name) {
var v = document.getElementsByTagName('pre');
var comment = this.comment();
var string = this.string();
var keywords = this.keywords();
var staticField = this.staticField();
var staticMethod = this.staticMethod();
for(var i = 0; i < v.length; i++) {
if('java' != v[i].id) {
continue;
}
var vi = v[i];
var s = vi.innerHTML.replace(/<br(?: \/)?>/ig, '\n');
vi.innerHTML = '';
var total = s.split('\n').length;
var ss = s.replace(comment, '<span id="comment">$1</span>')
.replace(string, '<span id="str">$1</span>')
.replace(keywords, '<span id="key">$1</span>')
.replace(staticField, '$1<span id="staticField">$2</span>')
.replace(staticMethod, '$1<span id="staticField">$2</span>');
s = this.init(ss);
var line = document.createElement('div');
line.innerHTML = this.toLine(total);
line.id = 'lineNumber';
vi.appendChild(line);
var code = document.createElement('div');
code.innerHTML = s;
code.id = 'java';
vi.appendChild(code);
vi.id = '';
}
}, // Java keywords list
keyList : 'abstract assert boolean break byte case catch char ' +
'class const continue default do double else enum extends ' +
'false final finally float for if goto implements import ' +
'instanceof int interface long native new null package ' +
'private protected public return short static strictfp ' +
'super switch synchronized this throw throws transient ' +
'true try void volatile while var arguments', // Converting non-HTML character to standard
init : function(str) {
return str.replace(/[ ]{2}/g, ' ')
.replace(/^[ ]/gm, ' ')
.replace(/\n\n/g, '<br> <br>')
.replace(/\n/g, '<br>')
.replace(/\t/g, ' ');
}, // Pattern of keyword
keywords : function() {
return new RegExp(this.getKeywords(this.keyList), "mg");
}, // Pattern of string constant
string : function() {
return /('[^'\\\r\n]*(?:\\.[^'\\\r\n]*)*')/g;
},
// Pattern of comment include sigleline and multiline
comment : function() {
return new RegExp(this._comment(), 'g');
// return /(\/\/[^'\r\n]*$|(?:[^']+)\/\*[\s\S]*?\*\/)/mg;
}, _comment : function() {
return '(/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/|//[^\n]*)';
}, // Converting keywords list to regex
getKeywords : function(keywords) {
return '\\b(' + keywords.replace(/ /g, '|') + ')\\b';
}, // Pattern of static method
staticMethod : function() {
return /([A-Z]\w+\.)(\w+\b)/g;
}, // Pattern of constant field
staticField : function() {
return /(^(?:[^'\\\r\n])*?|(?:'(?:[^'\\\r\n])+?'[^'\\\r\n]*?)*?)\b([A-Z_]+)\b(?![^']*?\\')/mg;
}, // Count number of lines
toLine : function(num) {
var s = '';
for(var i = 1; i <= num; i++) {
if(i > 1) {
s += '<br/>';
}
s += this.format(i);
}
return s;
}, // Formatting line number
format : function(num) {
if(num < 10) {
return '00' + num;
}else if(num < 100){
return '0' + num;
}else{
return num;
}
}
}
</script>
\".*?[^\\]\"
应该可以满足你要求。此正则在http://regexpal.com/ 上验证通过,匹配到"\t\t\fwwqf\"wefeasfdf" 和 "asfdsfd\"asfd" 两个