C#实现如下

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions; namespace DEMO
{
class Darrenstring
{
public class Matchpattern:Darrenstring
{
private string text;
private string pattern;
private int index;
public bool ismatch;
public Matchpattern(string Text, string Pattern)
{
text = Text;
pattern = Pattern;
Match a = Regex.Match(text,pattern);
index=a.Index;
ismatch = a.Success;
}
public int Index()
{
return index;
}
public string Before()
{
if (ismatch)
{
return (text.Substring(, index));
}
else return null;
}
public string After()
{
if (ismatch)
{
return (text.Substring(index + pattern.Length, text.Length - pattern.Length-index));
}
else return null;
}
}
}
}

类写好后 我们来做个试验 新建个winform程序

程序部分代码如下:

 private void Confirm_Click(object sender, EventArgs e)
{
Darrenstring.Matchpattern A = new Darrenstring.Matchpattern(Source.Text, Terminal.Text);
//Terminal.Text = Convert.ToString(A.Index());
before1.Text = A.Before();
After.Text = A.After();
IsMatched.Checked = A.ismatch;
}

运行后结果图如下:

效果不错功能实现

MSDN资料参考

System.String.RegularExpressions命名空间说明         https://msdn.microsoft.com/zh-cn/library/System.Text.RegularExpressions(v=vs.80).aspx

Regex类说明     https://msdn.microsoft.com/zh-cn/library/system.text.regularexpressions.regex(v=vs.80).aspx

最新文章

  1. div 添加滚动条
  2. Android SurfaceView的生命周期
  3. org.apache.hadoop.hdfs.server.namenode.SafeModeException: Cannot create directory /user/hive/warehouse/page_view. Name node is in safe mode
  4. 【CITE】C#默认以管理员身份运行程序实现代码
  5. POJ 1410 Intersection(判断线段交和点在矩形内)
  6. jquery foreach
  7. java图片处理
  8. PHP金字塔的输出
  9. BZOJ 1831 逆序对
  10. 【POJ3208】 (DP)
  11. 服务 Service 基本介绍
  12. 让asp.net网站支持多语言,使用资源文件
  13. 如何用Python在豆瓣中获取自己喜欢的TOP N电影信息
  14. Python 妙用heapq
  15. JAVA之旅(二十)—HashSet,自定义存储对象,TreeSet,二叉树,实现Comparator方式排序,TreeSet小练习
  16. MVC中一般为什么用IQueryable而不是用IList?
  17. 安装pyspider遇到的坑
  18. python 0007
  19. pythonGUI编程-tkinter
  20. Linux 文件系统介绍

热门文章

  1. showslow / YSlow for PhantomJS/slimerjs(gecko)/phantomas
  2. Java 之多线程通信(等待/唤醒)
  3. 洛谷 [BJOI2012]最多的方案
  4. 面试常见的selenium问题
  5. 《深入理解Linux网络技术内幕》阅读笔记 --- 路由表
  6. python逆向工程:通过代码生成类图
  7. 0406-服务注册与发现-客户端feign-使用、配置、日志、timeout
  8. 解决linux下python多版本兼容问题?
  9. CKEditor的下载、配置与使用
  10. beego——获取参数