Regex r; // 声明一个 Regex类的变量
r = new Regex("\s2000"); // 定义表达式
Regex r = new Regex("abc"); // 定义一个Regex对象实例
Match m = r.Match("123abc456"); // 在字符串中匹配
if (m.Success)
{
Console.WriteLine("Found match at position " + m.Index); //输入匹配字符的位置
}
MatchCollection mc;
String[] results = new String[20];
int[] matchposition = new int[20];
Regex r = new Regex("abc"); //定义一个Regex对象实例
mc = r.Matches("123abc4abcd");
for (int i = 0; i < mc.Count; i++) //在输入字符串中找到所有匹配
{
results = mc.Value; //将匹配的字符串添在字符串数组中
matchposition = mc.Index; //记录匹配字符的位置
}
using System;
using System.Text.RegularExpressions;
public class RegexTest
{
public static void RunTest()
{
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();
}
}
using System;
using System.Text.RegularExpressions;
public class RegexTest
{
public static void RunTest()
{
int counter;
Match m;
CaptureCollection cc;
GroupCollection gc;
Regex r = new Regex("(Abc)+"); //查找"Abc"
m = r.Match("XYZAbcAbcAbcXYZAbcAb"); //设定要查找的字符串
gc = m.Groups;
//输出查找组的数目
Console.WriteLine("Captured groups = " + gc.Count.ToString());
// Loop through each group.
for (int i=0; i < gc.Count; i++) //查找每一个组
{
cc = gc.Captures;
counter = cc.Count;
Console.WriteLine("Captures count = " + counter.ToString());
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
Regex r;
Match m;
CaptureCollection cc;
int posn, length;
r = new Regex("(abc)*");
m = r.Match("bcabcabc");
for (int i=0; m.Groups.Value != ""; i++)
{
cc = m.Groups.Captures;
for (int j = 0; j < cc.Count; j++)
{
posn = cc[j].Index; //捕获对象位置
length = cc[j].Length; //捕获对象长度
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有