public static void Run() { var startFolder = @"C:\Program Files (x86)\Microsoft Visual Studio\"; IEnumerable <FileInfo> fileList = GetFiles(startFolder); var searchTerm = new System.Text.RegularExpressions.Regex(@"Visual (Basic|C#|C\+\+|Studio)"); var queryMatchingFiles = from file in fileList where file.Extension.Contains(".htm") let fileText = File.ReadAllText(file.FullName) let matches = searchTerm.Matches(fileText) where matches.Count() > 0 select new { name = file.FullName, matchedValues = from match in matches select match.Value }; Console.WriteLine("The term \"{0}\" was found in:", searchTerm.ToString()); foreach (var v in queryMatchingFiles) { var s = v.name.Substring(startFolder.Length - 1); Console.WriteLine(s); foreach (var v2 in v.matchedValues) { Console.WriteLine(" " + v2); } } Console.ReadLine(); }
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\"; // Take a snapshot of the file system. IEnumerable <System.IO.FileInfo> fileList = GetFiles(startFolder); // Create the regular expression to find all things "Visual". System.Text.RegularExpressions.Regex searchTerm = new System.Text.RegularExpressions.Regex(@"Visual (Basic|C#|C\+\+|J#|SourceSafe|Studio)"); // 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 = System.IO.File.ReadAllText(file.FullName) let matches = searchTerm.Matches(fileText) where matches.Count > 0 select new { name = file.FullName, matchedValues = from System.Text.RegularExpressions.Match match in matches select match.Value }; // Execute the query. Console.WriteLine("The term \"{0}\" was found in:", searchTerm.ToString()); 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); // For this file, write out all the matching strings foreach (var v2 in v.matchedValues) { Console.WriteLine(" " + v2); } } // Keep the console window open in debug mode Console.WriteLine("Press any key to exit"); Console.ReadKey(); }
private void FilterList(ObjectListView olv, string searchText, bool showMatching = false) { // Construct the regex to search for string[] SearchedString = searchText.Split('*'); StringBuilder regex = new StringBuilder(); //regex.Append(".*"); foreach (string term in SearchedString) { if (term != string.Empty && Array.IndexOf(SearchedString, term) == (SearchedString.Length - 1)) { regex.Append(term); } else { regex.Append(term + ".*"); } } regex.Append(".*"); System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("^" + regex.ToString() + "$", System.Text.RegularExpressions.RegexOptions.IgnoreCase & System.Text.RegularExpressions.RegexOptions.Multiline); TextMatchFilter filter = null; if (!string.IsNullOrEmpty(searchText)) { filter = TextMatchFilter.Regex(olv, r.ToString()); } if (showMatching) { // Setup a default renderer to draw the filter matches if (filter == null) { olv.DefaultRenderer = null; } else { olv.DefaultRenderer = new HighlightTextRenderer(filter); } // Some lists have renderers already installed HighlightTextRenderer highlightingRenderer = olv.GetColumn(0).Renderer as HighlightTextRenderer; if (highlightingRenderer != null) { highlightingRenderer.Filter = filter; } olv.AdditionalFilter = filter; } else { olv.DefaultRenderer = null; olv.AdditionalFilter = filter; } }
public void RegEx() { //Modify this path as necessary so that it accesses your version Vistual Studio string startFolder = @"c:\program files (x86)\Microsoft Visual Studio 10.0\"; // Take a snapshot of the file system IEnumerable <System.IO.FileInfo> fileList = GetFiles(startFolder); //Create the regular expression to find all things "Visual" System.Text.RegularExpressions.Regex searchTerm = new System.Text.RegularExpressions.Regex(@"Visual (Basic|C#|C\+\+|J#|SourceSafe|Studio)"); //Search the contents of each .htm file. // Remove the where clause to find even more matchedValue! // This query produces a list of filed where a match // was found ,and a list of matchValues in that file. //Note: Explict typing of "Match" in select clause. // This is required because MatchCollection is not a // genertic IEnumerable collection var queryMatchingFiles = from file in fileList //设置文件查找范围 在设置的路经上 where file.Extension == ".htm" // 在设置的路径上上查找扩展名为“.htm”的文件 let fileText = System.IO.File.ReadAllText(file.FullName) //fileText为通过访问所设置路经上文件的全称进而访问文件的文本内容 let matches = searchTerm.Matches(fileText) //所查到的文本匹配的内容 where matches.Count > 0 //此时存在文本匹配成功的选项 select new { name = file.FullName, matchedValue = from System.Text.RegularExpressions.Match match in matches select match.Value }; // Execute the query Console.WriteLine("The term \"{0}\" was found in:", searchTerm.ToString()); 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); //For this file,write out all the matching strings foreach (var v2 in V.matchedValue) { Console.WriteLine(" " + v2); } //Keep the console windo open in debug mode Console.WriteLine("Press any key to exit"); Console.ReadKey(); } }
private void ValidateStringAsBundleIdentifier(string inputString) { if (string.IsNullOrEmpty(inputString)) { throw new System.FormatException("BundleIdentifier string formatted incorrectly. Must be non-empty."); } System.Text.RegularExpressions.Regex stringFormat = new System.Text.RegularExpressions.Regex("^com\\." + this.currentSettings.CompanyName + "\\.[\\w\\d]+$"); if (!stringFormat.IsMatch(inputString)) { throw new System.FormatException("BundleIdentifier string formatted incorrectly. Must be of the regex format: " + stringFormat.ToString()); } }
public void Add(System.Text.RegularExpressions.Regex rx, CharacterSet first, int priority) { if (rx == null) { throw new ArgumentNullException("rx"); } // avoid identical duplicates: string p = rx.ToString(); foreach (RegexRecognizerPattern rxp in _Patterns) { if (String.Equals(p, rxp.Pattern, StringComparison.Ordinal)) { return; } } RegexRecognizerPattern pattern = new RegexRecognizerPattern(rx, first, priority); _Patterns.Add(pattern); }
public override string ToString() { return(backingRegularExpression.ToString()); }
private void ValidateStringAsVersionNumber(string inputString) { if (string.IsNullOrEmpty(inputString)) { throw new System.FormatException("Version number is not a valid format. Must be non-empty"); } // Valid format of version number is ##.##.## or 1.12.1 string numDigitsPerIterationAsChar = NumDigitsPerVersionIteration.ToString(); string regexNoMoreThanXDigits = "[0-9]{1," + numDigitsPerIterationAsChar + "}"; System.Text.RegularExpressions.Regex stringFormat = new System.Text.RegularExpressions.Regex("^" + regexNoMoreThanXDigits + "\\." + regexNoMoreThanXDigits + "\\." + regexNoMoreThanXDigits + "$"); if (!stringFormat.IsMatch(inputString)) { throw new System.FormatException("Version number is not a valid format." + " Please match the format ##.##.##, or as a regular expression: " + stringFormat.ToString()); } }
private void ValidateStringAsVersionNumber(string inputString) { if (string.IsNullOrEmpty(inputString)) { throw new System.FormatException("Version number is not a valid format. Must be non-empty"); } // Valid format of version number is ##.##.## or 1.12.1 string numDigitsPerIterationAsChar = NumDigitsPerVersionIteration.ToString(); string regexNoMoreThanXDigits = "[0-9]{1," + numDigitsPerIterationAsChar + "}"; System.Text.RegularExpressions.Regex stringFormat = new System.Text.RegularExpressions.Regex("^" + regexNoMoreThanXDigits + "\\." + regexNoMoreThanXDigits + "\\." + regexNoMoreThanXDigits + "$"); if (!stringFormat.IsMatch(inputString)) { throw new System.FormatException("Version number is not a valid format." + " Please match the format ##.##.##, or as a regular expression: " + stringFormat.ToString()); } }
private void ValidateStringAsBundleIdentifier(string inputString) { if (string.IsNullOrEmpty(inputString)) { throw new System.FormatException("BundleIdentifier string formatted incorrectly. Must be non-empty."); } System.Text.RegularExpressions.Regex stringFormat = new System.Text.RegularExpressions.Regex("^com\\." + this.currentSettings.CompanyName + "\\.[\\w\\d]+$"); if (!stringFormat.IsMatch(inputString)) { throw new System.FormatException("BundleIdentifier string formatted incorrectly. Must be of the regex format: " + stringFormat.ToString()); } }