using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Net;
namespace 正則表達式
{
class Program
{
static void Main(string[] args)
{
//正則表達式
/*
* 元字符
.:b.g能匹配bug,big。。。。b..g能匹配biig,buug
[]:b[aui]g能匹配bag,bug,big,b[0-9]g則能匹配0-9的任意字符,當.出現在[]中,不作爲元字符
|:將兩個匹配條件進行邏輯運算,或
():將()之間括起來的表達式定義爲組,並且將匹配到的表達式保存到臨時區域,這個元字符在字符串提取時有用,
把一些字符表示爲一個整體。改變優先級,定義提取組兩個作用。
* :表示限定前面的表達式出現0次或多次,a*b-->aab,aaaab a.*b-->acb,adbdb,a(ab)*-->a,aab,aabab
+:匹配前面的子表達式一次或多次,9+-->9,99,999,zo+-->zo,zoo,不能匹配z
?:匹配前面子表達式0次多次,do(es)?-->do,does 一般用來匹配可選部分
{n}:匹配確定的n次,zo(2)-->zoo,e{2}-->不能匹配,seed{e}可以匹配到
{n,}:至少匹配n次
{n,m}:至少匹配n次,最多m次
限定符:
*/
//Regex.IsMatch();用來判斷給定字符串是否匹配某個正則表達式
//Regex.Match();用來從給定的字符串中按照正則表達式的要求提取【一個】匹配的字符串
//Regex.Matches();用來從給定的字符串中按照正則表達式的要求提取【所有】匹配的字符串
//Regex.Replace();替換所有正則表達式匹配的字符串爲另一個字符串
while (true)
{
string str = Console.ReadLine();
//注意,要想完全匹配就必須加^和$,否則只要有字符串中含有匹配則返回true
//驗證郵政編碼
//bool b = Regex.IsMatch(str,"^[0-9]{6}$");
//輸入[10-25]任意數字
//bool b = Regex.IsMatch(str,"^(1[0-9]|2[0-5])$");
//驗證手機號,\d爲任意數字
//bool b = Regex.IsMatch(str, @"^\d{11}$");
//匹配z或者food
//bool b = Regex.IsMatch(str,"z|food");
//匹配身份證
//長度爲15或18位,首位不爲0,15位時全部位數字,18位時最後一位爲xX或者數字
//寫法1:^([1-9][0-9]{14}[0-9]{2}[0-9Xx])?$
//寫法2:^([1-9][0-9]{14}[1-9][0-9]{16}[0-9X])$
//bool b = Regex.IsMatch(str,@"^([1-9][0-9]{14}[1-9][0-9]{16}[0-9X])$");
//.net默認使用Unicode的匹配方式
//bool b = Regex.IsMatch(str, @"\d+",RegexOptions.ECMAScript);
//bool b = Regex.IsMatch(str,@"[0-9]+");//這個能準確判斷是ASCII字符123,不包含Unicode字符123,其實就是輸入法中的全角輸出123
//bool b = Regex.IsMatch(str,@"\w+",RegexOptions.ECMAScript);
//判斷字符串是否爲國內電話,不考慮分機
// 010-9999999,010xxxxxxx
//0335-9999999,033599999999(區號+電話)
//12306 5位
//11位手機號
//bool b = Regex.IsMatch(str,@"^(\d{3,4}-7\d{7,8}|\d{5})$");
//匹配IP地址,4段用.分割的最多三位數字
//bool b = Regex.IsMatch(str,@"^([0-9]{1,3}\.){3}[1-9]{1,3}$");
//判斷是否合法的日期格式“2008-08-08”
//bool b = Regex.IsMatch(str,@"^[0-9]{4}-[0-9]{2}-[0-9]{2}$");
//限制月份1-12
//bool b = Regex.IsMatch(str,@"^[0-9]{4}-(0[1-9]|1[0-2])-[0-9]{2}$");
//判斷是否是合法url地址
//bool b = Regex.IsMatch(str,@"^[a-zA-z0-9]+://.+$");
//提取字符串
//一般提取不加^$
//Regex.Match只提取一個匹配Regex.Mathces提取所有匹配
//Match match = Regex.Match(str,"[0-9]+");
//獲取所有匹配項,提取html的網頁郵箱
//通過添加()就能實現提取組
//在正則表達式中,()既有改變優先級的作用又具有提取組的功能
//MatchCollection matcher = Regex.Matches(str, @"[-a-zA-Z_0-9.]+@[-a-zA-Z0-9_]+(\.[a-zA-Z]+)+");
//foreach (Match item in matcher) {
//item.Value表示本次提取到的字符串
//item.Groups集合中存儲的就是所有的分組信息
//item.Groups[0].Value與item.vlaue是等價的都表示本次提取到的完整的字符串,表示整個郵箱字符串,而
//item.Groups[i].Value則表示第一組的字符串
// Console.WriteLine(item.Value);
//}
//提取文件中的文件名
//此處因爲有“貪婪模式”的存在,所以\\肯定匹配最後一個\
//string path = @"C:\DRIVERS\WIN\risfds.txt";
//Match match = Regex.Match(path,@".+\\(.+)");
//Console.WriteLine(match.Groups[1].Value);
//從郵箱中提取用戶名和後綴[email protected]
Match match = Regex.Match(str,@"(.+)@(.+)");
Console.WriteLine("{0},{1}",match.Groups[1].Value,match.Groups[2].Value);
//從“192.168.10.5[prot=21,type=ftp]”這個字符串表示地址爲192.168.10.5的服務21端口提供的是ftp服務
//其中如果,type=ftp部分被省略,則默認爲http服務
Regex.Match(str,@"(.+)\[port=[0-9]{2,5}(,type=(.+))?\]");
Console.WriteLine("ip:{0}",match.Groups[1].Value);
Console.WriteLine("port:[0]",match.Groups[2].Value);
Console.WriteLine(match.Groups[4].Value.Length != 0 ? match.Groups[3].Value : "http");
//.+(貪婪模式)儘可能多的來匹配
//當在“限定符”後使用?表示終止貪婪模式
//終止貪婪模式,會儘可能少的匹配
string msg = "abccccc";
bool b = Regex.IsMatch(msg,@"^abc*?$");
//下載字符串
WebClient client = new WebClient();
string html = client.DownloadString("http://localhost:8080");
//從html字符串中提取郵件地址
MatchCollection matcher = Regex.Matches(html,@"[-a-zA-Z0-9_.]+@[-a-zA-Z0-9]+(\.[a-zA-Z]+)(1,2)");
foreach (Match item in matcher) {
Console.WriteLine(item.Value);
}
//提取網頁圖片
WebClient wb = new WebClient();
string html1 = wb.DownloadString("http://baidu.com");
//提取image標籤
MatchCollection matcher1 = Regex.Matches(html1, @"<img\s+alt="""" src=""(.+)"" \>", RegexOptions.IgnoreCase);
//通過提取組,獲取img的src屬性
foreach (Match item in matcher1)
{
string pathImg = "http://localhost:8080/" + item.Groups[1].Value;
wb.DownloadFile(pathImg, @"d:\" + System.DateTime.Now.ToFileTime());
}
//提取超鏈接
WebClient wb2 = new WebClient();
string html2 = wb.DownloadString("http://login");
MatchCollection matches2 = Regex.Matches(html2,@"<a\s*href="".+?"">.+?</a>",RegexOptions.IgnoreCase);
foreach (Match item in matches2) {
Console.WriteLine(item.Value);
}
//字符串替換
string msg1 = "aaaabbbbasdf";
msg1 = msg1.Replace("a","A");
//替換轉換的是格式
msg1 = Regex.Replace(msg1,@"a+","A");
//將hello 'welcome' to 'China'替換成 hello 【welcome】 to 【China】,如果替換成$1則"【$1】$$1"
string msg2 = "hello 'welcome' to 'China' 'iss' 'is'";
msg2 = Regex.Replace(msg2,"'(.+?)'","【$1】");
//隱藏手機號碼
string msg3 = "架飛機離開38369331964發了卡經適房的";
msg3 = Regex.Replace(msg,@"([0-9]{3})[0-9]{4}([0-9]{4})","$1****$2");
//隱藏郵箱名
string msg4 = "[email protected]";
msg4 = Regex.Replace(msg4,@"\w+(@\w+\.\w+)","****$1",RegexOptions.ECMAScript);
// \b表示單詞的邊界,單詞便是[a-zA-z0-9]
// \b是斷言的一種,只是判斷是否匹配並不真正匹配
// \b表示一邊是單詞,一邊是不是,不能兩邊都不是
//將row的單詞換成line
msg = Regex.Replace(msg,@"\brow\b","line");
//提取出3個字母的單詞
MatchCollection matches = Regex.Matches(msg,@"[a-z]{3}",RegexOptions.IgnoreCase);
//JJJJ-->輸出J
msg = Regex.Replace(msg,@"(.)\1+","$1");
}
Console.ReadKey();
//敏感詞過濾
//用來儲存需要審覈的關鍵詞
StringBuilder sbMode = new StringBuilder();
//用來儲存絕對禁止的關鍵詞
StringBuilder sbBanned = new StringBuilder();
string[] lines = File.ReadAllLines("1.txt");
for (int i = 0;i < lines.Length;i++) {
string[] parts = lines[i].Split('=');
if (parts[1] == "{MOD}") {
sbMode.AppendFormat("{0}|", parts[0]);
} else if (parts[1] == "{BANNED}") {
sbBanned.AppendFormat("{0}",parts[0]);
}
}
sbMode.Remove(sbMode.Length - 1,1);
sbBanned.Remove(sbBanned.Length - 1,1);
string userInput = "";
if (Regex.IsMatch(userInput, sbBanned.ToString())) {
} else if (Regex.IsMatch(userInput,sbMode.ToString())) {
}
//抓取網絡招聘信息
//信息頭
//<a href="http://" onclick="zzSearch.acStatRecJob(1);" class="jobname" target="_blank">項目經理</a>
//
WebClient wc = new WebClient();
string html = wc.DownloadString("http://localhost.htm");
MatchCollection match = Regex.Matches(html,"<a href=\"http://search.51job.com/job/46614662,c.html\" onclick=\"zzSearch.acStatRecJob(1);\" class=\"jobname\" target=\"_blank\">(.+?)</a>");
foreach (Match item in match) {
Console.WriteLine(item.Groups[1].Value);
}
}
}
}
正則表達式的應用
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.