C#正则表达式Regex常用匹配
使用Regex类需要引用命名空间:using System.Text.RegularExpressions;
利用Regex类实现验证
示例1:注释的代码所起的作用是相同的,不过一个是静态方法,一个是实例方法
var source = "刘备关羽张飞孙权何问起";
//Regex regex = new Regex("孙权");
//if (regex.IsMatch(source))
//{
// Console.WriteLine("字符串中包含有敏感词:孙权!");
//}
if (Regex.IsMatch(source, "孙权"))
{
Console.WriteLine("字符串中包含有敏感词:孙权!");
}
Console.ReadLine();
示例2:使用带两个参数的构造函数,第二个参数指示忽略大小写,很常用
var source = "123abc345DEf";
Regex regex = new Regex("def",RegexOptions.IgnoreCase);
if (regex.IsMatch(source))
{
Console.WriteLine("字符串中包含有敏感词:def!");
}
Console.ReadLine();
使用Regex类进行替换
示例1:简单情况
var source = "123abc456ABC789";
// 静态方法
//var newSource=Regex.Replace(source,"abc","|",RegexOptions.IgnoreCase);
// 实例方法
Regex regex = new Regex("abc", RegexOptions.IgnoreCase);
var newSource = regex.Replace(source, "|");
Console.WriteLine("原字符串:"+source);
Console.WriteLine("替换后的字符串:" + newSource);
Console.ReadLine();
结果:
原字符串:123abc456ABC789
替换后的字符串:123|456|789
示例2:将匹配到的选项替换为html代码,我们使用了MatchEvaluator委托
var source = "123abc456ABCD789";
Regex regex = new Regex("[A-Z]{3}", RegexOptions.IgnoreCase);
var newSource = regex.Replace(source,new MatchEvaluator(OutPutMatch));
Console.WriteLine("原字符串:"+source);
Console.WriteLine("替换后的字符串:" + newSource);
Console.ReadLine(); //柔城 private static string OutPutMatch(Match match)
{
return "<b>" +match.Value+ "</b>";
}
输出:
原字符串:123abc456ABCD789
替换后的字符串:123<b>abc</b>456<b>ABC</b>D789
C#正则表达式Regex常用匹配
在线测试:http://tool.hovertree.com/a/zz/
#region 身份证号码正则表达式
//何问起 Console.WriteLine("请输入一个身份证号码");
string id = Console.ReadLine();
bool b4 = Regex.IsMatch(id, @"^\d{15}|\d{18}$");
bool b5 = Regex.IsMatch(id, @"^(\d{15}|\d{18})$");
Console.WriteLine(b4);
Console.WriteLine(b5); #endregion #region 匹配电话号码
//hovertree Console.WriteLine("请输入电话号码");
string phone = Console.ReadLine();
bool b = Regex.IsMatch(phone, @"^((\d{3,4}\-\d?{7,8})|(\d{5}))$");
Console.WriteLine(b); #endregion #region 匹配email的regex //hovertree Console.WriteLine("请输入Email地址");
string email = Console.ReadLine();
bool bhvt = Regex.IsMatch(email, @"^\w+@\w+\.\w+$");
Console.WriteLine(bhvt); #endregion #region 匹配ip地址的regex
//hovertree Console.WriteLine("请输入一个IP地址");
string ip = Console.ReadLine();
bool bkly = Regex.IsMatch(ip, @"^\d{1,3}(\.\d{1,3}){3}$");
Console.WriteLine(bkly); #endregion #region 匹配日期合法regex
//何问起 Console.WriteLine("请输入一个日期");
string date = Console.ReadLine();
bool bhovertree = Regex.IsMatch(date, @"^\d{4}\-\d{1,2}\-\d{1,2}$");
Console.WriteLine(bhovertree); #endregion #region 匹配url地址的regex
//"http://hovertree.com"
//"http://keleyi.com/a/bjae/h1o76nuh.htm?id=3&name=aaa"
//"https://s.taobao.com/search?q=hover+tree&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20151204&ie=utf8"
//"ftp://127.0.0.1/myslider.txt" //hovertree Console.WriteLine("请输入url地址");
string url = Console.ReadLine();
bool bkeleyi = Regex.IsMatch(url, @"^[a-zA-Z]+://.+$");
Console.WriteLine(bkeleyi); #endregion
ASP.NET开源CMS http://www.cnblogs.com/sosoft/p/cms.html
开发技术文章收集 http://www.cnblogs.com/sosoft/p/kaifajishu.html
C#正则表达式Regex常用匹配的更多相关文章
- C# 筛选string 类型里面的汉字,获取首字母字母,正则表达式Regex 常用验证
界面效果 1.提取汉字 private void buttonX1_Click(object sender, EventArgs e) { if (TxtYuan.Text.Trim() != &qu ...
- Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答
Boost正则表达式库regex常用search和match示例 - 编程语言 - 开发者第2241727个问答 Boost正则表达式库regex常用search和match示例 发表回复 Boo ...
- C#正则表达式Regex类的用法
C#正则表达式Regex类的用法 更多2014/2/18 来源:C#学习浏览量:36891 学习标签: 正则表达式 Regex 本文导读:正则表达式的本质是使用一系列特殊字符模式,来表示某一类字符串, ...
- 正则表达式Regex
1.概念 正则表达式,又称规则表达式.(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念.正则表通常被用来检索.替换那些符合某个模式( ...
- 正则表达式(RegEx)官方手册/权威指南【Python】
前言 正则表达式(称为RE,或正则,或正则表达式模式)本质上是嵌入在Python中的一种微小的.高度专业化的编程语言,可通过 re 模块获得. 使用这种小语言,你可以为要匹配的可能字符串集指定规则:此 ...
- js正则表达式验证、匹配数字、匹配字符串、匹配中文、匹配任意字符备忘录
本文转自:91博客 :原文地址:http://www.9191boke.com/235792704.html 正则表达式或“regex”用于匹配字符串的各个部分,下面是我创建正则表达式的备忘录.包括一 ...
- 正则表达式 regex
正则表达式存在于String api下的matches方法 常用正常表达式: 字符 x 字符 x \\ 反斜线字符 字符类 [abc] a.b 或 c(简单类) [^abc] 任何字符,除了 a.b ...
- pytho day6 <正则表达式、常用模块、反射>
本节介绍: 一:正则表达式: 正则表达并不是python 独有的.在各个语言里都有该语法的介绍.正则表达是处理字符串的强大的处理工具.拥有自己的独特的 处理方法.和处理引擎.虽然性能没有python ...
- C# 正则表达式及常用正则表达式
元字符 描述 .点 匹配任何单个字符.例如正则表达式r.t匹配这些字符串:rat.rut.r t,但是不匹配root. $ 匹配行结束符.例如正则表达式weasel$ 能够匹配字符串"He' ...
随机推荐
- MYSQL INNODB PAGE一督
MYSQL INNODB PAGE一督
- linux查看发行版本
redhat系 cat /etc/redhat-release 其他 lsb_release -a
- 给你的应用“一只”智慧的眼睛 —— Barcode常识普及以及识别信息处理
在“如何用MediaCapture解决二维码扫描问题”这篇文章中,我们通过“成像”.“截图”与“识别”三个步骤介绍了使用MediaCapture扫码的主要过程及注意事项.本文主要针对“识别”的过程,对 ...
- Meteor+AngularJS:超快速Web开发
为了更好地描述Meteor和AngularJS为什么值得一谈,我先从个人角度来回顾一下这三年来WEB开发的变化: 三年前,我已经开始尝试前后端分离,后端使用php的轻量业务逻辑框架.但 ...
- python实现grep
import sys import os import re def usage(): print "[Usage]: python grep.py filename grepString. ...
- [译]libev和libevent的设计差异
本文译自what's the difference between libev and libevent? 作者是libev作者 [问]两个库都是为异步io调度而设计,在Linux上都是使用epoll ...
- [Linux] linux下安装配置 zookeeper/redis/solr/tomcat/IK分词器 详细实例.
今天 不知自己装的centos 出现了什么问题, 一直卡在 启动界面, 找了半天没找见原因(最后时刻还是发现原因, 只因自己手欠一怒之下将centos删除了, 而且选择的是在本地磁盘也删除. ..让我 ...
- windows7查看最近使用记录
1.看计算机在哪天运行过~运行了多久! C:\Windows\SchedLgU.txt 2.看你最近运行过什么程序: C:\Windows\Prefetch 3.看你最近打开过什么文件(非程序)和文件 ...
- salesforce 零基础学习(五十二)Trigger使用篇(二)
第十七篇的Trigger用法为通过Handler方式实现Trigger的封装,此种好处是一个Handler对应一个sObject,使本该在Trigger中写的代码分到Handler中,代码更加清晰. ...
- Leetcode-24 Swap Nodes in Pairs
#24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...