Pattern IDpattern = Pattern.compile("([a-zA-Z_][a-zA-Z0-9_]*)");
上面的代码可以匹配一个标示符,我想要这个表达式也能匹配数字,字符串
那我的正则表达式应该怎样写
是否是这样:
([a-zA-Z_][a-zA-Z0-9_]*) | ([0-9]+)) | (\"*\")可是这样写有问题,不是很明白java的正则表达式的书写方法,和形式预言书上的不一样,希望大虾们可以解答
上面的代码可以匹配一个标示符,我想要这个表达式也能匹配数字,字符串
那我的正则表达式应该怎样写
是否是这样:
([a-zA-Z_][a-zA-Z0-9_]*) | ([0-9]+)) | (\"*\")可是这样写有问题,不是很明白java的正则表达式的书写方法,和形式预言书上的不一样,希望大虾们可以解答
你是要匹配什么样字符串了?
按你这个([a-zA-Z_][a-zA-Z0-9_]*) ¦ ([0-9]+)) ¦ (\"*\") 表达的意思是"[\\w\\d_]*"匹配字母、数字和_
\t 间隔 ('\u0009')
\n 换行 ('\u000A')
\r 回车 ('\u000D')
\d 数字 等价于[0-9]
\D 非数字 等价于[^0-9]
\s 空白符号 [\t\n\x0B\f\r]
\S 非空白符号 [^\t\n\x0B\f\r]
\w 单独字符 [a-zA-Z_0-9]
\W 非单独字符 [^a-zA-Z_0-9]
\f 换页符
\e Escape
\b 一个单词的边界
\B 一个非单词的边界
\G 前一个匹配的结束^为限制开头
^java 条件限制为以Java为开头字符
$为限制结尾
java$ 条件限制为以java为结尾字符
.为限制一个任意字符
java.. 条件限制为java后除换行外任意两个字符
加入特定限制条件「[]」
[a-z] 条件限制在小写a to z范围中一个字符
[A-Z] 条件限制在大写A to Z范围中一个字符
[a-zA-Z] 条件限制在小写a to z或大写A to Z范围中一个字符
[0-9] 条件限制在小写0 to 9范围中一个字符
[0-9a-z] 条件限制在小写0 to 9或a to z范围中一个字符
[0-9[a-z]] 条件限制在小写0 to 9或a to z范围中一个字符(交集)[]中加入^后加再次限制条件「[^]」
[^a-z] 条件限制在非小写a to z范围中一个字符
[^A-Z] 条件限制在非大写A to Z范围中一个字符
[^a-zA-Z] 条件限制在非小写a to z或大写A to Z范围中一个字符
[^0-9] 条件限制在非小写0 to 9范围中一个字符
[^0-9a-z] 条件限制在非小写0 to 9或a to z范围中一个字符
[^0-9[a-z]] 条件限制在非小写0 to 9或a to z范围中一个字符(交集)在限制条件为特定字符出现0次以上时,可以使用「*」
J* 0个以上J
.* 0个以上任意字符
J.*D J与D之间0个以上任意字符在限制条件为特定字符出现1次以上时,可以使用「+」
J+ 1个以上J
.+ 1个以上任意字符
J.+D J与D之间1个以上任意字符在限制条件为特定字符出现有0或1次以上时,可以使用「?」
JA? J或者JA出现
regex = "[a-zA-Z0-9_]*";
or
regex = "\\w*";
转成Java的写法:\"[^\\\\\"]*(\\\\.[^\\\\\"]*)*\"可以匹配:"asdfasf"、"adfs\"asdf"、"\"\"\""、"asdfasf\\\"asdfasdf" 等式样的字符串像这种正则表达式都是大师级别的人物写出来的,而且是最优化的,普通人很难做到的。
有些 bug,不过能对大多数的代码进行高亮处理,把下面的代码粘贴一下就
用浏览器打开就能看到效果了。test.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="author" content="CSDN bao110908">
<script type="text/javascript" src="javascript.js"></script>
<title>Java正则表达式教程</title>
</head>
<style type="text/css">
#lineNumber {
font-family: "Courier New";
font-size: 10pt;
line-height: 14.07pt;
border: solid 1px #d3d3d3;
float: left;
color: #f5f5f5;
background-color: #d3d3d3;
letter-spacing: 0.02em;
padding-top: 15px;
padding-bottom: 15px;
width: 40px;
text-align: center;
}
#java {
background-color: #f5f5f5;
padding-left: 10px;
padding-top: 15px;
padding-bottom: 15px;
border-top: solid 1px #d3d3d3;
border-right: solid 1px #d3d3d3;
border-bottom: solid 1px #d3d3d3;
margin-left: 40px;
margin-right: 10px;
font-family: "Courier New";
font-size: 10pt;
line-height: 14pt;
letter-spacing: 0.02em;
}
#key {
font-weight: bold;
color: #008ed2;
}
#str {
color: #ff8080;
}
#comment {
color: #32cd32;
}
#staticField {
color: #008ed2;
font-style: italic;
}
</style> <body>
<pre name="java" id="java">package com.csdn.j2se;import java.util.Calendar;/**
* 测试类
*/
public class Test { /**
* main 方法,入口函数
* @param args
*/
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, Calendar.APRIL);
calendar.set(Calendar.DATE, 1);
// 输出 2008 年 4 月 1 日现在的时间
System.out.println(calendar);
// 在控制台上输出文字
System.out.println("Hello World!");
// 进行循环
for(int i = 0; i < 10; i++) {
System.out.println(i);
}
}
}</pre> </body>
</html>
<script type="text/javascript">
syntax.replace("java");
</script>javascript.js
var syntax = { // Java code with syntax higher light,
// and add line number.
replace : function(name) {
var v = document.getElementsByName(name);
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++) {
var vi = v[i];
var s = vi.innerHTML;
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", // Converting non-HTML character to standard
init : function(str) {
return str.replace(/[ ]{2}/g, " ")
.replace(/^[ ]/gm, " ")
.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 /(\/\/[^"\r\n]*$|(?:[^"]+)\/\*[\s\S]*?\*\/)/mg;
}, // 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;
}
}
}
return /(\/\/[^"\r\n]*$|(?:[^"]+)\/\*[\s\S]*?\*\/)/mg;
}, 改为下面这样子,多行注释就可以了。 comment : function() {
return /(\/\/[^"\r\n]*$|\/\*[\s\S]*?\*\/)/mg;
},
Pattern IDpattern = Pattern.compile("([a-zA-Z0-9_]*)"); //这样就能匹配数字了
[\code]
还有阿,正则表达式没有想象的那么难,学一下形式语言就可以写出一些个很常用的了,复杂点的也不困难Pattern.compile("(\\\".*\\\")|([a-zA-Z_][a-zA-Z0-9_]*)|([0-9]+)|([\\+][\\+])|([\\-][\\-])|([\\+])|([\\-])|([\\*])|([/])|([%])|([=][=])|([!][=])|([>][=])|([<][=])|([>])|([<])|([=])|([\\(])|([\\)])|([\\[])|([\\]])|([\\{])|([\\}])|([;])|([,])|([&][&])|([|][|])|([!])|([&])|([|])");
这就是我想要的正则表达式:)