正则表达式提取练习2
2012-08-15 18:06
169 查看
using System;
using System.Collections.Generic;
using
System.Linq;
using System.Text;
using
System.Text.RegularExpressions;
namespace 正则表达式提取练习
{
class
Program
{
static void Main(string[] args)
{
#region 从路径中提取文件名
//string str =
@"c:\a\a\a\a\a\a\a\a.sql\a.sql\a.sql\a.sql\b.txt";
////这种思路不合适。
////Match match = Regex.Match(str,
@"\\\w+\.\w+");
////Console.WriteLine(match.Value);
//Match match =
Regex.Match(str,
@"^[a-zA-Z]:(\\\w+\.*\w*)+\\(.+)$");//写一个正则表达式与文件路径完全匹配。然后提取组
//if (match.Success)
//{
//
Console.WriteLine(match.Groups[2].Value);
//}
//Console.ReadKey();
#endregion
#region 从“June 26 ,
1951”中提取出月份June来。@"([a-zA-Z]+)\s+\d{1,2},\s*\d{4}"进行匹配。
//string str = "June 26 , 1951";
////通过提取组的方式,将所要的数据提取出来
//Match match = Regex.Match(str,
@"^([a-zA-Z]+)\s*(\d{1,2})\s*,\s*(\d{4})$");
//if
(match.Success)
//{
//
Console.WriteLine(match.Groups[1].Value);
//
Console.WriteLine(match.Groups[2].Value);
//
Console.WriteLine(match.Groups[3].Value);
//}
//Console.ReadKey();
#endregion
#region
“192.168.10.5[port=21,type=ftp]”,这个字符串表示IP地址为192.168.10.5的服务器的21端口提供的是ftp服务,其中如果“,type=ftp”部分被省略,则默认为http服务。请用程序解析此字符串,然后打印出“IP地址为***的服务器的***端口提供的服务为***”
////192.168.10.5[port=21]
//// string msg =
"192.168.10.5[port=21,type=ftp]";
//string msg =
"192.168.10.5[port=21]";
////[0-9]
//Match match =
Regex.Match(msg,
@"^((\d{1,3}\.){3}\d{1,3})\[port=(\d+)(,type=([a-zA-Z]+))?\]$");
//if (match.Success)
//{
//
Console.WriteLine("ip:{0}", match.Groups[1].Value);
//
Console.WriteLine("port:{0}", match.Groups[3].Value);
//
Console.WriteLine("services:{0}", (match.Groups[5].Value.Length == 0) ? "http" :
match.Groups[5].Value);
//}
//Console.ReadKey();
#endregion
}
}
}
using System.Collections.Generic;
using
System.Linq;
using System.Text;
using
System.Text.RegularExpressions;
namespace 正则表达式提取练习
{
class
Program
{
static void Main(string[] args)
{
#region 从路径中提取文件名
//string str =
@"c:\a\a\a\a\a\a\a\a.sql\a.sql\a.sql\a.sql\b.txt";
////这种思路不合适。
////Match match = Regex.Match(str,
@"\\\w+\.\w+");
////Console.WriteLine(match.Value);
//Match match =
Regex.Match(str,
@"^[a-zA-Z]:(\\\w+\.*\w*)+\\(.+)$");//写一个正则表达式与文件路径完全匹配。然后提取组
//if (match.Success)
//{
//
Console.WriteLine(match.Groups[2].Value);
//}
//Console.ReadKey();
#endregion
#region 从“June 26 ,
1951”中提取出月份June来。@"([a-zA-Z]+)\s+\d{1,2},\s*\d{4}"进行匹配。
//string str = "June 26 , 1951";
////通过提取组的方式,将所要的数据提取出来
//Match match = Regex.Match(str,
@"^([a-zA-Z]+)\s*(\d{1,2})\s*,\s*(\d{4})$");
//if
(match.Success)
//{
//
Console.WriteLine(match.Groups[1].Value);
//
Console.WriteLine(match.Groups[2].Value);
//
Console.WriteLine(match.Groups[3].Value);
//}
//Console.ReadKey();
#endregion
#region
“192.168.10.5[port=21,type=ftp]”,这个字符串表示IP地址为192.168.10.5的服务器的21端口提供的是ftp服务,其中如果“,type=ftp”部分被省略,则默认为http服务。请用程序解析此字符串,然后打印出“IP地址为***的服务器的***端口提供的服务为***”
////192.168.10.5[port=21]
//// string msg =
"192.168.10.5[port=21,type=ftp]";
//string msg =
"192.168.10.5[port=21]";
////[0-9]
//Match match =
Regex.Match(msg,
@"^((\d{1,3}\.){3}\d{1,3})\[port=(\d+)(,type=([a-zA-Z]+))?\]$");
//if (match.Success)
//{
//
Console.WriteLine("ip:{0}", match.Groups[1].Value);
//
Console.WriteLine("port:{0}", match.Groups[3].Value);
//
Console.WriteLine("services:{0}", (match.Groups[5].Value.Length == 0) ? "http" :
match.Groups[5].Value);
//}
//Console.ReadKey();
#endregion
}
}
}
相关文章推荐
- Java 使用正则表达式提取字符串制定的字符
- java正则表达式提取需要的字符并放入数组;ArrayList数组去重复
- 【学习python】re 正则表达式匹配特定词性的conll,提取句子主干(主谓宾)
- Java 正则表达式提取标签中的属性(src 连接地址)等
- 正则表达式练习: android 歌词解析lrc 转 txt 代码
- [转] javascript 正则表达式提取数字使用
- 使用Java正则表达式提取数据
- 基于Python正则表达式提取搜索结果中的站点地址
- 正则表达式提取String子串
- 简单正则表达式练习
- Dave Python 练习十七 -- 正则表达式
- 正则表达式提取链接地址-邮箱邮件
- js正则表达式提取字符串中的数字
- java中 利用正则表达式提取( )内内容
- 从短信中提取短信验证码正则表达式,缺少足够测试
- 28-正则表达式-09-正则表达式(练习_3)
- javascipt-正则表达式练习--验证电话号码
- 正则表达式练习
- PHP中使用正则表达式提取中文实现笔记