TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Regex, Read and Match File Lines

Process a text file by using Regex on each line. Use the StreamReader class.
Regex, file. A file can be parsed with Regex. The Regex can process each line to find all matching parts. This is useful for log files or output from other programs.RegexFile
Here we process a file with regular expressions. The data file is provided—you will need to create it and access it in the test program.
An example. To use a regular expression on a file you must first read in the file into a string. Here's a console program that opens a StreamReader on the file and reads in each line.StreamReader

Note: The ReadLine() method will return each line separately, or null if there are no more data.

ReadLine, ReadLineAsync

Then: We put the regular expression logic into the StreamReader code to parse an entire file. We match each line.

Tip: Groups is indexed starting at 1. Never access Groups[0], which can result in lots of grief as your algorithm does not work.

C# program that matches lines using System; using System.IO; using System.Text.RegularExpressions; class Program { static void Main() { Regex regex = new Regex(@"\s/Content/([a-zA-Z0-9\-]+?)\.aspx"); // "\s/Content/" : space and then Content directory // "([a-zA-Z0-9\-]+?) : group of alphanumeric characters and hyphen // ? : don't be greedy, match lazily // \.aspx : file extension required for match using (StreamReader reader = new StreamReader(@"C:\programs\log.txt")) { string line; while ((line = reader.ReadLine()) != null) { // Try to match each line against the Regex. Match match = regex.Match(line); if (match.Success) { // Write original line and the value. string v = match.Groups[1].Value; Console.WriteLine(line); Console.WriteLine("... " + v); } } } } } Contents, log.txt: 2008-10-16 23:56:44 W3SVC2915713 GET /Content/String.aspx - 80 66.249 2008-10-16 23:59:50 W3SVC2915713 GET /Content/Trim-String-Regex.aspx - 80 66.249 Output 2008-10-16 23:56:44 W3SVC2915713 GET /Content/String.aspx - 80 66.249 ... String 2008-10-16 23:59:50 W3SVC2915713 GET /Content/Trim-String-Regex.aspx - 80 66.249 ... Trim-String-Regex
Notes, file. Please create the log.txt file and place it somewhere and read it in with the above program. It is important to have the test file to see how the Regex code works.
Further usages. There are more usages of this kind of code in programs. We can match lines in files such as logs, trace files, scientific calculations, CSV files, or any text file.

Tip: Processing each line separately may be faster because less memory must be accessed and fewer characters must be checked.

Review: We combined the StreamReader class with the Regex class in the base class library to parse large text files.

A summary. We used a regular expression on every line in a text file. We showed an accurate and simple way of matching every line in a text file.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf