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# RegexOptions.Multiline

Use Regex.Matches and RegexOptions.Multiline to simplify handling multiple lines at once.
RegexOptions.Multiline. This Regex option makes handling multiple lines easier. With it, we can apply a regular expression over an input string that has many lines.
Details, problem. Each line in the input string should be tested against the entire regular expression. To restate, a newline means a separate part of the string.Regex
An example. We deal with specific control characters in the regular expression pattern. We can use the "^" char to specify the start of the input string, and "$" to specify the end of it.

Example: We specify one or more characters between the start and the end of each line. The plus means "one or more."

Capture: The pattern matches each line in a capture. We use 2 foreach-loops to enumerate the results and print them to the screen.

Foreach

Info: We see how the RegexOptions.Multiline argument was passed as the third parameter to the Regex.Matches static method.

C# program that uses RegexOptions.Multiline using System; using System.Text.RegularExpressions; class Program { static void Main() { // Input string. const string example = @"This string has two lines"; // Get a collection of matches with the Multiline option. MatchCollection matches = Regex.Matches(example, "^(.+)$", RegexOptions.Multiline); foreach (Match match in matches) { // Loop through captures. foreach (Capture capture in match.Captures) { // Display them. Console.WriteLine("--" + capture.Value); } } } } Output --This string --has two lines
Notes, RegexOptions. We can use an enumerated constant with Regex methods. There are other RegexOptions you can consider. For complicate problems, a RegexOptions enum is often needed.RegexOptions.CompiledRegexOptions.IgnoreCase
A summary. RegexOptions.Multiline is useful for treating each separate line in an input string as a separate input. This can make dealing with long input strings much easier.
© 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