您的位置:首页 > 其它

正则表达式引擎

2006-06-29 13:00 169 查看
本页将针对几种编程环境,举一些使用正则表达式的简单示例。

1. 首先是 JavaScript 中的正则表达式

1.1 正则表达式的创建
在代码中写正则表达式常量:var re = /pattern/igm;
通过字符串生成正则表达式:var re = new RegExp("pattern", "igm");

支持的参数参数:i:IgnoreCase,g:Global,m:Multiline
不支持 Singleline。详情参见本站基础文档(3.5)

1.2 查找匹配
JavaScript 正则表达式的匹配函数是 exec,用法:

re.lastIndex = 0;
var match = re.exec( string );

var beginPos = match.index;
var endPos = match.lastIndex;

执行 exec 之前,可以给 re.lastIndex 赋一个初始值,使表达式从该位置开始匹配。如果表达式有 Global 属性,那么 exec 成功之后,lastIndex 属性会被修改为本次匹配结果的结束位置,作为下一次 exec 的开始位置。

返回来的 match 是一个数组对象,记录匹配的结果。使用 match[0] 则得到 re 所匹配到的字符串,match.index 记录匹配到的字符串的开始位置,match.lastIndex 记录结束位置。可以使用 match
就得到第n组括号内匹配到的字符串。

1.3 替换
JavaScript 中,替换的用法是:

var result = string.replace( re, "replaceto" );

在 "replaceto" 字符串中,使用 "$1","$2" 就可以引用表达式中括号内匹配到的内容。

1.4 示例
<script language=javascript>

var re = //$(/d+)/;
var string = "it costs $200";

var match = re.exec( string );

alert( "found: " + match[0] );
alert( "position: " + match.index );
alert( "$1: " + match[1] );

var result = string.replace( re, "¥$1" );

alert( result );

</script>
如果不想使用 alert 的方法来查看变量值,推荐参考 [客户端脚本调试方法] 进行单步调试运行。

2. VBScript 中的正则表达式

2.1 正则表达式的创建
VBScript 中,正则表达式需要几个语句完成:

Dim re ' 声明
Set re = new RegExp ' 创建

re.IgnoreCase = true
re.Multiline = true
re.Global = true

re.Pattern = "pattern" ' 正则表达式

不支持 Singleline 属性。详情参见本站基础文档(3.5)

2.2 查找匹配
VBScript 中,正则表达式匹配的方法是 re.Execute( str )。与 JavaScript 中的正则表达式不同的是,如果 Global 属性为 true,那么 Execute 方法会一次性返回所有的匹配结果:

Dim Matches, Match
……
re.Global = true
Set Matches = re.Execute( str)
For Each Match in Matches
beginPos = Match.FirstIndex
value = Match.Value
Next

通过访问 Matches 中的每一个 Match 对象,来获取表达式的匹配结果。在一个 Match 对象中,第n组括号内匹配到的内容,可以使用 Match.SubMatches(n-1) 来获取。

2.3 替换
替换的用法如下:

Dim result
result = re.Replace( str, "replaceto" )

需要注意,变量 re 和 str 的位置与在 JavaScript 中是不同的。

2.4 示例
<script language=vbscript>
Dim re, str, Matches, Match, result
Set re = new RegExp

re.Global = true
re.Pattern = "/$(/d+)"

str = "it costs $200"

Set Matches = re.Execute( str )

For Each Match in Matches
MsgBox "found: " & Match.Value
MsgBox "position: " & Match.FirstIndex
MsgBox "$1: " & Match.Submatches(0)
Next

result = re.Replace( str, "¥$1" )

MsgBox result
</script>
2.5 在 Visual Basic 6.0 中的使用正则表达式

在“项目”菜单上单击“引用”。

双击“Microsoft VBScript Regular Expressions 5.5”,然后单击“确定”。

在 VB 代码中,声明变量:
Dim re As RegExp, oMatch As Match, oMatches As MatchCollection

其他的用法与在 VBScript 中类似。详情参见微软网站上的文档:

HOW TO:在 Microsoft Visual Basic 6.0 中使用正则表达式

3. Java 中使用正则表达式

3.1 正则表达式的创建
JDK 中自带正则表达式引擎(java.util.regex)是从 1.4 版本开始的,以前的版本如果需要正则表达式,需要使用第三方提供的库。而微软提供的虚拟机 Java VM 停留在 1.1 版本,因此,在微软提供的 Java 虚拟机中也没有自带正则表达式引擎。

使用 java.util.regex 的方法如下:

import java.util.*;
......

Pattern p = Pattern.compile("//$(//d+)");
Matcher m = p.matcher("it costs $200");

Pattern 的 matcher() 方法只是得到了一个包含“字符串信息”和“表达式信息”的对象,而并没有进行任何的匹配或者其他操作。

3.2 查找匹配
对字符串的匹配以及其他操作,将在 Matcher 对象上进行:

boolean found = m.find();
if( found )
{
String foundstring = m.group();
int beginPos = m.start();
int endPos = m.end();

String found1 = m.group(1); // 括号内匹配内容
}

如果要从指定位置开始匹配,可以使用 m.find(pos)。

3.3 替换
替换操作也是在 Matcher 对象上进行:

String result = m.replaceAll("¥$1");

得到的 result 是一个新字符串,不影响原来的字符串。

3.4 示例
import java.util.regex.*;

public class RegexExample {

public static void main(String[] args)
{
Pattern p = Pattern.compile("//$(//d+)");
Matcher m = p.matcher("it costs $200");

boolean found = m.find();
if( found )
{
String foundstring = m.group();
System.out.println(foundstring);

int beginPos = m.start();
int endPos = m.end();
System.out.println("start:" + beginPos + "/nend:" + endPos);

String found1 = m.group(1); // 括号内匹配内容
System.out.println(found1);
}

String result = m.replaceAll("¥$1");
System.out.println(result);
}
}
代码已调试通过。

4. dotNET 中的正则表达式

 

5. Visual C++ 中使用正则表达式

[GRETA 正则表达式库]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: