C#正则表达式类

Regex

Regex 类表示不可变(只读)正则表达式类。它还包含各种静态方法,允许在不显式创建其他类的实例的情况下使用其他正则表达式类。

下面的代码示例创建了 Regex 类的实例并在初始化对象时定义一个简单的正则表达式。请注意,使用了附加的反斜杠作为转义字符,它将\s 匹配字符类中的反斜杠指定为原义字符。

  
  
// Declare object variable of type Regex. Regex r; // Create a Regex object and define its regular expression. r = new Regex( " \\s2000 " );

Match

Match 类表示正则表达式匹配操作的结果。下面的示例使用 Regex 类的Match 方法返回 Match 类型的对象,以便找到输入字符串中的第一个匹配项。此示例使用 Match 类的 Match.Success 属性来指示是否已找到匹配。

  
  
// Create a new Regex object. Regex r = new Regex( " abc " ); // Find a single match in the string. Match m = r.Match( " 123abc456 " ); if (m.Success) { // Print out the character position where a match was found. // (Character position 3 in this case.) Console.WriteLine( " Found match at position " + m.Index); }

MatchCollection

MatchCollection 类表示成功的非重叠匹配的序列。该集合为不可变(只读)的,并且没有公共构造函数。MatchCollection 的实例是由Regex.Matches 方法返回的。

下面的示例使用 Regex 类的 Matches 方法,通过在输入字符串中找到的所有匹配填充MatchCollection。该示例将此集合复制到一个字符串数组和一个整数数组中,其中字符串数组用以保存每个匹配项,整数数组用以指示每个匹配项的位置。

  
  
MatchCollection mc; String[] results = new String[ 20 ]; int [] matchposition = new int [ 20 ]; // Create a new Regex object and define the regular expression. Regex r = new Regex( " abc " ); // Use the Matches method to find all matches in the input string. mc = r.Matches( " 123abc4abcd " ); // Loop through the match collection to retrieve all // matches and positions. for ( int i = 0 ; i < mc.Count; i ++ ) { // Add the match string to the string array. results[i] = mc[i].Value; // Record the character position where the match was found. matchposition[i] = mc[i].Index; }

GroupCollection

GroupCollection 类表示捕获的组的集合并返回单个匹配中捕获的组的集合。该集合为不可变(只读)的,并且没有公共构造函数。GroupCollection 的实例在Match.Groups 属性返回的集合中返回。

以下控制台应用程序示例查找并输出由正则表达式捕获的组的数目。有关如何提取组集合的每一成员中的各个捕获项的示例,请参见下面一节的 Capture Collection示例。

  
  
using System; using System.Text.RegularExpressions; public class RegexTest { public static void RunTest() { // Define groups "abc", "ab", and "b". Regex r = new Regex( " (a(b))c " ); Match m = r.Match( " abdabc " ); Console.WriteLine( " Number of groups found = " + m.Groups.Count); } public static void Main() { RunTest(); } }

该示例产生下面的输出。

 
 
Number of groups found = 3

CaptureCollection

CaptureCollection 类表示捕获的子字符串的序列,并且返回由单个捕获组执行的捕获的集合。由于限定符,捕获组可以在单个匹配中捕获多个字符串。Captures 属性(CaptureCollection类的对象)是作为 Matchgroup 类的成员提供的,以便于对捕获的子字符串的集合的访问。

例如,如果使用正则表达式 ((a(b))c)+(其中 + 限定符指定一个或多个匹配)从字符串“abcabcabc”中捕获匹配,则子字符串的每一匹配的GroupCaptureCollection 将包含三个成员。

以下控制台应用程序示例使用正则表达式 (Abc)+ 来查找字符串“XYZAbcAbcAbcXYZAbcAb”中的一个或多个匹配。该示例阐释了使用Captures 属性来返回多组捕获的子字符串。

  
  
using System; using System.Text.RegularExpressions; public class RegexTest { public static void RunTest() { int counter; Match m; CaptureCollection cc; GroupCollection gc; // Look for groupings of "Abc". Regex r = new Regex( " (Abc)+ " ); // Define the string to search. m = r.Match( " XYZAbcAbcAbcXYZAbcAb " ); gc = m.Groups; // Print the number of groups. Console.WriteLine( " Captured groups = " + gc.Count.ToString()); // Loop through each group. for ( int i = 0 ; i < gc.Count; i ++ ) { cc = gc[i].Captures; counter = cc.Count; // Print number of captures in this group. Console.WriteLine( " Captures count = " + counter.ToString()); // Loop through each capture in group. for ( int ii = 0 ; ii < counter; ii ++ ) { // Print capture and position. Console.WriteLine(cc[ii] + " Starts at character " + cc[ii].Index); } } } public static void Main() { RunTest(); } }
此示例返回下面的输出结果。
  
  
Captured groups = 2 Captures count = 1 AbcAbcAbc Starts at character 3 Captures count = 3 Abc Starts at character 3 Abc Starts at character 6 Abc Starts at character 9

group 类表示来自单个捕获组的结果。因为 Group 可以在单个匹配中捕获零个、一个或更多的字符串(使用限定符),所以它包含Capture 对象的集合。因为 Group 继承自 Capture,所以可以直接访问最后捕获的子字符串(Group 实例本身等价于 Captures 属性返回的集合的最后一项)。

通过对由 Groups 属性返回的 GroupCollection 对象进行索引返回 Group 的实例。索引器可以是组号,在使用 "(?<groupname>)" 分组构造时则是捕获组的名称。例如,可以在 C# 代码中使用Match.Groups[groupnum]Match.Groups["groupname"],或者在 Visual Basic代码中使用 Match.Groups(groupnum)Match.Groups("groupname")

下面的代码示例使用嵌套的分组构造来将子字符串捕获到组中。

  
  
int [] matchposition = new int [ 20 ]; String[] results = new String[ 20 ]; // Define substrings abc, ab, b. Regex r = new Regex( " (a(b))c " ); Match m = r.Match( " abdabc " ); for ( int i = 0 ; m.Groups[i].Value != "" ; i ++ ) { // Copy groups to string array. results[i] = m.Groups[i].Value; // Record character position. matchposition[i] = m.Groups[i].Index; }
此示例返回下面的输出结果。
  
  
results[ 0 ] = " abc " matchposition[ 0 ] = 3 results[ 1 ] = " ab " matchposition[ 1 ] = 3 results[ 2 ] = " b " matchposition[ 2 ] = 4
下面的代码示例使用命名的分组构造,从包含“DATANAME:VALUE”格式的数据的字符串中捕获子字符串,正则表达式通过冒号“:”拆分数据。
  
  
Regex r = new Regex( " ^(?<name>\\w+):(?<value>\\w+) " ); Match m = r.Match( " Section1:119900 " );
此正则表达式返回下面的输出结果。
  
  
m.Groups[ " name " ].Value = " Section1 " m.Groups[ " value " ].Value = " 119900 "

Capture

Capture 类包含来自单个子表达式捕获的结果。

下面的示例在 Group 集合中循环,从 Group 的每一成员中提取 Capture 集合,并且将变量 posn length 分别分配给找到每一字符串的初始字符串中的字符位置,以及每一字符串的长度。

  
  
Regex r; Match m; CaptureCollection cc; int posn, length; r = new Regex( " (abc)+ " ); m = r.Match( " bcabcabc " ); for ( int i = 0 ; m.Groups[i].Value != "" ; i ++ ) { // Capture the Collection for Group(i). cc = m.Groups[i].Captures; for ( int j = 0 ; j < cc.Count; j ++ ) { // Position of Capture object. posn = cc[j].Index; // Length of Capture object. length = cc[j].Length; } }
基于SSM框架的智能家政保洁预约系统,是一个旨在提高家政保洁服务预约效率和管理水平的平台。该系统通过集成现代信息技术,为家政公司、家政服务人员和消费者提供了一个便捷的在线预约和管理系统。 系统的主要功能包括: 1. **用户管理**:允许消费者注册、登录,并管理他们的个人资料和预约历史。 2. **家政人员管理**:家政服务人员可以注册并更新自己的个人信息、服务别和服务时间。 3. **服务预约**:消费者可以浏览不同的家政服务选项,选择合适的服务人员,并在线预约服务。 4. **订单管理**:系统支持订单的创建、跟踪和管理,包括订单的确认、完成和评价。 5. **评价系统**:消费者可以在家政服务完成后对服务进行评价,帮助提高服务质量和透明度。 6. **后台管理**:管理员可以管理用户、家政人员信息、服务别、预约订单以及处理用户反馈。 系统采用Java语言开发,使用MySQL数据库进行数据存储,通过B/S架构实现用户与服务的在线交互。系统设计考虑了不同用户角色的需求,包括管理员、家政服务人员和普通用户,每个角色都有相应的权限和功能。此外,系统还采用了软件组件化、精化体系结构、分离逻辑和数据等方法,以便于未来的系统升级和维护。 智能家政保洁预约系统通过提供一个集中的平台,不仅方便了消费者的预约和管理,也为家政服务人员提供了一个展示和推广自己服务的机会。同时,系统的后台管理功能为家政公司提供了强大的数据支持和决策辅助,有助于提高服务质量和管理效率。该系统的设计与实现,标志着家政保洁服务向现代化和网络化的转型,为管理决策和控制提供保障,是行业发展中的重要里程碑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值