代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
将 LINQ 查询与正则表达式合并在一起
作者:
DDT
/ 发布于
2012/10/8
/
563
使用 Regex 类创建正则表达式以便在文本字符串中进行更复杂的匹配。
<div>using System.IO; using System.Text; class QueryWithRegEx { public static void Main() { // Modify this path as necessary so that it accesses your version of Visual Studio. string startFolder = @"c:\program files\Microsoft Visual Studio 9.0\"; // One of the following paths may be more appropriate on your computer. //string startFolder = @"c:\program files (x86)\Microsoft Visual Studio 9.0\"; //string startFolder = @"c:\program files\Microsoft Visual Studio 10.0\"; //string startFolder = @"c:\program files (x86)\Microsoft Visual Studio 10.0\";</div> <div> // Take a snapshot of the file system. IEnumerable<FileInfo> fileList = GetFiles(startFolder);</div> <div> // Create the regular expression to find all things "Visual". RegularExpressions.Regex searchTerm = new RegularExpressions.Regex(@"Visual (Basic|C#|C\+\+|J#|SourceSafe|Studio)");</div> <div> // Search the contents of each .htm file. // Remove the where clause to find even more matchedValues! // This query produces a list of files where a match // was found, and a list of the matchedValues in that file. // Note: Explicit typing of "Match" in select clause. // This is required because MatchCollection is not a // generic IEnumerable collection. var queryMatchingFiles = from file in fileList where file.Extension == ".htm" let fileText = File.ReadAllText(file.FullName) let matches = searchTerm.Matches(fileText) where matches.Count > 0 select new { name = file.FullName, matchedValues = from RegularExpressions.Match match in matches select match.Value };</div> <div> // Execute the query. Console.WriteLine("The term \"{0}\" was found in:", searchTerm.ToString());</div> <div> foreach (var v in queryMatchingFiles) { // Trim the path a bit, then write // the file name in which a match was found. string s = v.name.Substring(startFolder.Length - 1); Console.WriteLine(s);</div> <div> // For this file, write out all the matching strings foreach (var v2 in v.matchedValues) { Console.WriteLine(" " + v2); } } <div> // Keep the console window open in debug mode Console.WriteLine("Press any key to exit"); Console.ReadKey(); } <div> // This method assumes that the application has discovery // permissions for all folders under the specified path. static IEnumerable<System.IO.FileInfo> GetFiles(string path) { if (!System.IO.Directory.Exists(path)) throw new DirectoryNotFoundException();</div> <div> string[] fileNames = null; List<FileInfo> files = new List<FileInfo>();</div> <div> fileNames = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories); foreach (string name in fileNames) { files.Add(new FileInfo(name)); } return files; } }
试试其它关键字
LINQ
查询与正则表达式
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
DDT
贡献的其它代码
(
160
)
.
Oracle统计表的数据行和数据块信息
.
html标签闭合检测与修复
.
Powershell日期计算
.
Powershell的Base64编解码
.
Powershell并行循环
.
Powershell目录中搜索文本
.
Powershell枚举远程机器上的本地权限组
.
VBScript解析csv文件
.
快速排序之Powershell
.
批处理输出格式化时间字符串
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3