\\\\(?<TheServer>\w*)\\(?<TheService>\w*)\
where the named group TheServer will match any server name within a UNC string, and TheService will match any service name within a UNC string. You need to store the groups that are returned by this regular expression in a keyed collection (such as a Hashtable) in which the key is the group name. Read the rest of this entry »
Popularity: 1% [?]
In this article, we will talk about finding one or more substrings corresponding to a particular pattern within a string using Regular Expression. You need to be able to inform the searching code to return either all matching substrings or only the matching substrings that are unique within the set of all matched strings. Read the rest of this entry »
Popularity: 1% [?]
There are many mechanisms for recording state information about applications, other than creating a file full of the information. One example of this type of mechanism is the Windows Event Log, where informational, security, and error states can be logged during an application’s progress. One of the primary reasons for creating a log file is to assist in troubleshooting or to debug your code in the field. If you are shipping code without some sort of debugging mechanism for your support staff (or possibly for you in a small company), we suggest you consider adding some logging support. Any developer who has spent a late night debugging a problem on a QA machine, or worse yet, at a customer site, can tell you the value of a log of the program’s actions. Read the rest of this entry »
Popularity: 2% [?]
You need to create a file-possibly for logging information to or storing temporary information-and then write information to it. You also need to be able to read the information that you wrote to this file. To create, write to, and read from a log file, we will use the FileStream and its reader and writer classes. For example, we will create methods to allow construction, reading to, and writing from a log file. To create a log file, you can use the following code: Read the rest of this entry »
Popularity: 3% [?]
Every line ends with a special character. For Windows files, the line terminating characters are a carriage return followed by a linefeed. This sequence of characters is described by the regular expression pattern \r\n. Unix files terminate their lines with just the linefeed character (\n). The regular expression “\n” is the lowest common denominator for both sets of line-terminating characters. Consequently, this method runs a regular expression that looks for the pattern “\n” in a string or file. The method code is shown below:
using System; using System.Text.RegularExpressions; using System.IO; public static long LineCount(string source, bool isFileName) { if (source != null) { string text = source; if (isFileName) { FileStream FS = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader SR = new StreamReader(FS); text = SR.ReadToEnd(); SR.Close(); FS.Close(); } Regex RE = new Regex("\n", RegexOptions.Multiline); MatchCollection theMatches = RE.Matches(text); // Needed for files with zero length // Note that a string will always have a line terminator // and thus will always have a length of 1 or more if (isFileName) { return (theMatches.Count); } else { return (theMatches.Count) + 1; } } else { // Handle a null source here return (0); } }
An alternative version of this method uses the StreamReader.ReadLine method to count lines in a file and a regular expression to count lines in a string:
public static long LineCount2(string source, bool isFileName) { if (source != null) { string text = source; long numOfLines = 0; if (isFileName) { FileStream FS = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read); StreamReader SR = new StreamReader(FS); while (text != null) { text = SR.ReadLine(); if (text != null) { ++numOfLines; } } SR.Close(); FS.Close(); return (numOfLines); } else { Regex RE = new Regex("\n", RegexOptions.Multiline); MatchCollection theMatches = RE.Matches(text); return (theMatches.Count + 1); } } else { // Handle a null source here return (0); } }
The following method counts the lines within a specified text file and a specified string:
public static void TestLineCount() { // Count the lines within the file TestFile.txt LineCount(@"C:\TestFile.txt", true); // Count the lines within a string // Notice that a \r\n characters start a new line // as well as just the \n character LineCount("Line1\r\nLine2\r\nLine3\nLine4", false); }
Simply running this regular expression against a string returns the number of lines minus one because the last line does not have a line-terminating character. To account for this, one is added to the final count of linefeeds in the string.
The LineCount method accepts two parameters. The first is a string that either contains the actual text that will have its lines counted or the path and name of a text file whose lines are to be counted. The second parameter, isFileName, determines whether the first parameter (source) is a string or a file path. If this parameter is true, the source parameter is a file path; otherwise, it is simply a string.
Macintosh files usually end with a carriage-return character (\r). To count the number of lines in this type of file, the regular expression should be changed to the following in the constructor of the Regex object:
Regex RE = new Regex("\r", RegexOptions.Multiline);
Popularity: 1% [?]
Compiling regular expressions allows the expression to run faster. To understand how, we need to examine the process that an expression goes through as it is run against a string. If an expression is not compiled, the regular expression engine converts the expression to a series of internal codes that are recognized by the regular expression engine; it is not converted to MSIL. As the expression runs against a string, the engine interprets the series of internal codes. This can be a slow process, especially as the source string becomes very large and the expression becomes much more complex. Read the rest of this entry »
Popularity: 2% [?]
You need to replace character patterns within the target string with a new string. However, in this case, each replacement operation has a unique set of conditions that must be satisfied in order to allow the replacement to occur. Consider, for example, that you receive a string in the form of XML (or possibly HTML). You wish to modify an attribute of a specific XML tag to a particular number, but only if that number is within a specified range (or possibly outside of a particular range). Read the rest of this entry »
Popularity: 2% [?]