SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com
Strings and Regular Expressions Prepared By : Abed ElAzeem Bukhari What’s in This Chapter? . Building strings . Formatting expressions . Using regular expressions
Examining System.String string message1 = "Hello"; // returns "Hello" message1 += ", There"; // returns "Hello, There" string message2 = message1  +  "!"; // returns "Hello, There!“ C# also allows extraction of a particular character using an indexer-like syntax: string message = "Hello"; char char4 = message[4]; // returns 'o'. Note the string is zero-indexed
System.String methods
building strings Encoder.cs Encoder2.cs
building strings cont. The  StringBuilder  class has two main properties: ➤  Length , which indicates the length of the string that it actually contains ➤  Capacity , which indicates the maximum length of the string in the memory allocation StringBuilder myBuilder = new StringBuilder("Hello for all ",  100 ); myBuilder Append(“students.");
stringbuilder members StringBuilder sb = new StringBuilder("Hello"); Or  you can create an empty StringBuilder with a given capacity: StringBuilder sb = new StringBuilder(20); // another way to set the capacity StringBuilder sb = new StringBuilder("Hello"); sb.Capacity = 100; a read-only MaxCapacity property : StringBuilder sb = new StringBuilder(100,  500 );
stringbuilder members cont
Format Strings double d = 16.45; int i = 25; Console.WriteLine("The double is {0,10:E} and the int contains {1}", d, i);
Format Strings cont ,[object Object],[object Object],[object Object]
How the string is formatted? Console.WriteLine("The double is {0,10:E} and the int contains {1}", d, i); // Likely implementation of Console.WriteLine() public void WriteLine(string format, object arg0, object arg1) { this.WriteLine(string.Format(this.FormatProvider, format, new object[]{arg0, arg1})); }
How the string is formatted?
The formattableVector example FormattableVector.cs The format specifiers you are going to support are: -  N  — Should be interpreted as a request to supply a quantity known as the Norm of the Vector. This is just the sum of squares of its components, which for mathematics buffs happens to be equal to the square of the length of the Vector, and is usually displayed between double vertical bars, like this: ||34.5||. -  VE  — Should be interpreted as a request to display each component in scientific format, just as the specifier E applied to a double indicates  (2.3E+01, 4.5E+02, 1.0E+00). -  IJK  — Should be interpreted as a request to display the vector in the form  23i + 450j + 1k . -Anything else should simply return the default representation of the Vector ( 23, 450, 1.0) .
Regular Expressions System.Text.RegularExpressions The regular expressions language is designed specifically for string processing. It contains two features: ➤  A set of  escape codes for identifying specific types of characters. You will be familiar with the use of the * character to represent any substring in DOS expressions. (For example, the DOS command  Dir Re*  lists the files with names beginning with Re .) Regular expressions use many sequences like this to represent items such as  any one character , a word break , one optional character , and so on. ➤  A system for grouping parts of substrings and intermediate results during a search operation.
Regular Expressions cont With regular expressions, you can perform quite  sophisticated and high- level operations on strings. For example, you can: ➤  Identify (and perhaps either flag or remove) all repeated words in a string (for example, “T he computer books books ” to “ The computer books ” ) ➤  Convert all words to title case (for example, “ this is a Title ” to “ This Is A Title ” ) ➤  Convert all words longer than three characters to title case (for example, “ this is a Title ” to “ This is a Title ” ) ➤  Ensure that sentences are properly capitalized ➤  Separate the various elements of a URI (for example, given http://www.najahclub.net , extract the protocol, computer name, file name, and so on)
Regular Expressions Examples const string pattern = "ion"; MatchCollection myMatches = Regex.Matches(myText, pattern, RegexOptions.IgnoreCase  | RegexOptions.ExplicitCapture ); foreach (Match nextMatch in myMatches) { Console.WriteLine(nextMatch.Index); } Another example in RegularExpressions.cs
Regular Expressions cont const string pattern = @"n"; MatchCollection myMatches = Regex.Matches(myText, pattern, RegexOptions.IgnoreCase  | RegexOptions.ExplicitCapture );
Regular Expressions patterns
Regular Expressions   Matches, Groups, and Captures For example, URIs have the format  <protocol> ://<address> :<port>  , where the port is  optional . An example of this is  http://www.el-bukhari.com:4355  .  Suppose that you want to extract the protocol, the address, and the port from a URI, where you know that there may or may not be whitespace (but no punctuation) immediately following the URI. You could do so using this expression: (+)://([^:]+)(?::(+))?
Thanks For Attending Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (18)

101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions
 
Ruby quick ref
Ruby quick refRuby quick ref
Ruby quick ref
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Ruby cheat sheet
Ruby cheat sheetRuby cheat sheet
Ruby cheat sheet
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrep
 
3.7 search text files using regular expressions
3.7 search text files using regular expressions3.7 search text files using regular expressions
3.7 search text files using regular expressions
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressions
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 
Php basics
Php basicsPhp basics
Php basics
 
Regex posix
Regex posixRegex posix
Regex posix
 
Data type2 c
Data type2 cData type2 c
Data type2 c
 
Regular expressions and php
Regular expressions and phpRegular expressions and php
Regular expressions and php
 
Enumerated data types in C
Enumerated data types in CEnumerated data types in C
Enumerated data types in C
 
Python revision tour i
Python revision tour iPython revision tour i
Python revision tour i
 
101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions101 3.7 search text files using regular expressions
101 3.7 search text files using regular expressions
 
C programming session 08
C programming session 08C programming session 08
C programming session 08
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
Programming in C
Programming in CProgramming in C
Programming in C
 

Ähnlich wie Csharp4 strings and_regular_expressions

Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingRasan Samarasinghe
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and StringsEduardo Bergavera
 
CSE 220 Assignment 3 Hints and Tips Some hints for approa.docx
CSE 220 Assignment 3 Hints and Tips  Some hints for approa.docxCSE 220 Assignment 3 Hints and Tips  Some hints for approa.docx
CSE 220 Assignment 3 Hints and Tips Some hints for approa.docxfaithxdunce63732
 
Java căn bản - Chapter9
Java căn bản - Chapter9Java căn bản - Chapter9
Java căn bản - Chapter9Vince Vo
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaghu nath
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)SURBHI SAROHA
 
It’s sometimes useful to make a little language for a simple problem.pdf
It’s sometimes useful to make a little language for a simple problem.pdfIt’s sometimes useful to make a little language for a simple problem.pdf
It’s sometimes useful to make a little language for a simple problem.pdfarri2009av
 
C programming day#3.
C programming day#3.C programming day#3.
C programming day#3.Mohamed Fawzy
 
Generating parsers using Ragel and Lemon
Generating parsers using Ragel and LemonGenerating parsers using Ragel and Lemon
Generating parsers using Ragel and LemonTristan Penman
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++K Durga Prasad
 
Bsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c languageBsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c languageRai University
 
Btech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c languageBtech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c languageRai University
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c languageRai University
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)sachindane
 

Ähnlich wie Csharp4 strings and_regular_expressions (20)

Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
 
CSE 220 Assignment 3 Hints and Tips Some hints for approa.docx
CSE 220 Assignment 3 Hints and Tips  Some hints for approa.docxCSE 220 Assignment 3 Hints and Tips  Some hints for approa.docx
CSE 220 Assignment 3 Hints and Tips Some hints for approa.docx
 
Java căn bản - Chapter9
Java căn bản - Chapter9Java căn bản - Chapter9
Java căn bản - Chapter9
 
2.regular expressions
2.regular expressions2.regular expressions
2.regular expressions
 
Structures
StructuresStructures
Structures
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
It’s sometimes useful to make a little language for a simple problem.pdf
It’s sometimes useful to make a little language for a simple problem.pdfIt’s sometimes useful to make a little language for a simple problem.pdf
It’s sometimes useful to make a little language for a simple problem.pdf
 
C programming day#3.
C programming day#3.C programming day#3.
C programming day#3.
 
Generating parsers using Ragel and Lemon
Generating parsers using Ragel and LemonGenerating parsers using Ragel and Lemon
Generating parsers using Ragel and Lemon
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Bsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c languageBsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c language
 
Btech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c languageBtech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c language
 
C programming language
C programming languageC programming language
C programming language
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
 

Mehr von Abed Bukhari

Csharp4 arrays and_tuples
Csharp4 arrays and_tuplesCsharp4 arrays and_tuples
Csharp4 arrays and_tuplesAbed Bukhari
 
Csharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsCsharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsAbed Bukhari
 
Csharp4 operators and_casts
Csharp4 operators and_castsCsharp4 operators and_casts
Csharp4 operators and_castsAbed Bukhari
 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_typesAbed Bukhari
 
Csharp4 inheritance
Csharp4 inheritanceCsharp4 inheritance
Csharp4 inheritanceAbed Bukhari
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4Abed Bukhari
 

Mehr von Abed Bukhari (8)

Csharp4 arrays and_tuples
Csharp4 arrays and_tuplesCsharp4 arrays and_tuples
Csharp4 arrays and_tuples
 
Csharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_eventsCsharp4 delegates lambda_and_events
Csharp4 delegates lambda_and_events
 
Csharp4 generics
Csharp4 genericsCsharp4 generics
Csharp4 generics
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
Csharp4 operators and_casts
Csharp4 operators and_castsCsharp4 operators and_casts
Csharp4 operators and_casts
 
Csharp4 objects and_types
Csharp4 objects and_typesCsharp4 objects and_types
Csharp4 objects and_types
 
Csharp4 inheritance
Csharp4 inheritanceCsharp4 inheritance
Csharp4 inheritance
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 

Csharp4 strings and_regular_expressions

  • 1. Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com
  • 2. Strings and Regular Expressions Prepared By : Abed ElAzeem Bukhari What’s in This Chapter? . Building strings . Formatting expressions . Using regular expressions
  • 3. Examining System.String string message1 = &quot;Hello&quot;; // returns &quot;Hello&quot; message1 += &quot;, There&quot;; // returns &quot;Hello, There&quot; string message2 = message1 + &quot;!&quot;; // returns &quot;Hello, There!“ C# also allows extraction of a particular character using an indexer-like syntax: string message = &quot;Hello&quot;; char char4 = message[4]; // returns 'o'. Note the string is zero-indexed
  • 6. building strings cont. The StringBuilder class has two main properties: ➤ Length , which indicates the length of the string that it actually contains ➤ Capacity , which indicates the maximum length of the string in the memory allocation StringBuilder myBuilder = new StringBuilder(&quot;Hello for all &quot;, 100 ); myBuilder Append(“students.&quot;);
  • 7. stringbuilder members StringBuilder sb = new StringBuilder(&quot;Hello&quot;); Or you can create an empty StringBuilder with a given capacity: StringBuilder sb = new StringBuilder(20); // another way to set the capacity StringBuilder sb = new StringBuilder(&quot;Hello&quot;); sb.Capacity = 100; a read-only MaxCapacity property : StringBuilder sb = new StringBuilder(100, 500 );
  • 9. Format Strings double d = 16.45; int i = 25; Console.WriteLine(&quot;The double is {0,10:E} and the int contains {1}&quot;, d, i);
  • 10.
  • 11. How the string is formatted? Console.WriteLine(&quot;The double is {0,10:E} and the int contains {1}&quot;, d, i); // Likely implementation of Console.WriteLine() public void WriteLine(string format, object arg0, object arg1) { this.WriteLine(string.Format(this.FormatProvider, format, new object[]{arg0, arg1})); }
  • 12. How the string is formatted?
  • 13. The formattableVector example FormattableVector.cs The format specifiers you are going to support are: - N — Should be interpreted as a request to supply a quantity known as the Norm of the Vector. This is just the sum of squares of its components, which for mathematics buffs happens to be equal to the square of the length of the Vector, and is usually displayed between double vertical bars, like this: ||34.5||. - VE — Should be interpreted as a request to display each component in scientific format, just as the specifier E applied to a double indicates (2.3E+01, 4.5E+02, 1.0E+00). - IJK — Should be interpreted as a request to display the vector in the form 23i + 450j + 1k . -Anything else should simply return the default representation of the Vector ( 23, 450, 1.0) .
  • 14. Regular Expressions System.Text.RegularExpressions The regular expressions language is designed specifically for string processing. It contains two features: ➤ A set of escape codes for identifying specific types of characters. You will be familiar with the use of the * character to represent any substring in DOS expressions. (For example, the DOS command Dir Re* lists the files with names beginning with Re .) Regular expressions use many sequences like this to represent items such as any one character , a word break , one optional character , and so on. ➤ A system for grouping parts of substrings and intermediate results during a search operation.
  • 15. Regular Expressions cont With regular expressions, you can perform quite sophisticated and high- level operations on strings. For example, you can: ➤ Identify (and perhaps either flag or remove) all repeated words in a string (for example, “T he computer books books ” to “ The computer books ” ) ➤ Convert all words to title case (for example, “ this is a Title ” to “ This Is A Title ” ) ➤ Convert all words longer than three characters to title case (for example, “ this is a Title ” to “ This is a Title ” ) ➤ Ensure that sentences are properly capitalized ➤ Separate the various elements of a URI (for example, given http://www.najahclub.net , extract the protocol, computer name, file name, and so on)
  • 16. Regular Expressions Examples const string pattern = &quot;ion&quot;; MatchCollection myMatches = Regex.Matches(myText, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture ); foreach (Match nextMatch in myMatches) { Console.WriteLine(nextMatch.Index); } Another example in RegularExpressions.cs
  • 17. Regular Expressions cont const string pattern = @&quot;n&quot;; MatchCollection myMatches = Regex.Matches(myText, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture );
  • 19. Regular Expressions Matches, Groups, and Captures For example, URIs have the format <protocol> ://<address> :<port> , where the port is optional . An example of this is http://www.el-bukhari.com:4355 . Suppose that you want to extract the protocol, the address, and the port from a URI, where you know that there may or may not be whitespace (but no punctuation) immediately following the URI. You could do so using this expression: (+)://([^:]+)(?::(+))?
  • 20. Thanks For Attending Abed El-Azeem Bukhari (MCPD,MCTS and MCP) el-bukhari.com

Hinweis der Redaktion

  1. //Encoder.cs using System; namespace Encoder { class Program { static void Main() { string greetingText = &amp;quot;Hello from all the students at Najah. &amp;quot;; greetingText += &amp;quot;We do hope you enjoy this codes as much as we enjoyed writing it&amp;quot;; for (int i = (int)&apos;z&apos;; i &gt;= (int)&apos;a&apos;; i--) { char old1 = (char)i; char new1 = (char)(i + 1); greetingText = greetingText.Replace(old1, new1); } for (int i = (int)&apos;Z&apos;; i &gt;= (int)&apos;A&apos;; i--) { char old1 = (char)i; char new1 = (char)(i + 1); greetingText = greetingText.Replace(old1, new1); } Console.WriteLine(&amp;quot;Encoded:\\n&amp;quot; + greetingText); Console.ReadLine(); } } } //Encoder2.cs using System; using System.Text; namespace Encoder2 { class Program { static void Main() { StringBuilder greetingBuilder = new StringBuilder(&amp;quot;Hello from all the students at Najah. &amp;quot;, 150); greetingBuilder.Append(&amp;quot;We do hope you enjoy this codes as much as we enjoyed writing it&amp;quot;); for(int i = (int)&apos;z&apos;; i&gt;=(int)&apos;a&apos; ; i--) { char old1 = (char)i; char new1 = (char)(i+1); greetingBuilder = greetingBuilder.Replace(old1, new1); } for(int i = (int)&apos;Z&apos;; i&gt;=(int)&apos;A&apos; ; i--) { char old1 = (char)i; char new1 = (char)(i+1); greetingBuilder = greetingBuilder.Replace(old1, new1); } Console.WriteLine(&amp;quot;Encoded:\\n&amp;quot; + greetingBuilder); Console.ReadLine(); } } }
  2. // FormattableVector.cs using System; using System.Text; namespace Najah.ILoveCsharp.FormattableVector { class MainEntryPoint { static void Main() { Vector v1 = new Vector(1,32,5); Vector v2 = new Vector(845.4, 54.3, -7.8); Console.WriteLine(&amp;quot;\\nIn IJK format,\\nv1 is {0,30:IJK}\\nv2 is {1,30:IJK}&amp;quot;, v1, v2); Console.WriteLine(&amp;quot;\\nIn default format,\\nv1 is {0,30}\\nv2 is {1,30}&amp;quot;, v1, v2); Console.WriteLine(&amp;quot;\\nIn VE format\\nv1 is {0,30:VE}\\nv2 is {1,30:VE}&amp;quot;, v1, v2); Console.WriteLine(&amp;quot;\\nNorms are:\\nv1 is {0,20:N}\\nv2 is {1,20:N}&amp;quot;, v1, v2); } } struct Vector : IFormattable { public double x, y, z; public Vector(double x, double y, double z) { this.x = x; this.y = y; this.z = z; } public string ToString(string format, IFormatProvider formatProvider) { if (format == null) return ToString(); string formatUpper = format.ToUpper(); switch (formatUpper) { case &amp;quot;N&amp;quot;: return &amp;quot;|| &amp;quot; + Norm() + &amp;quot; ||&amp;quot;; case &amp;quot;VE&amp;quot;: return String.Format(&amp;quot;( {0:E}, {1:E}, {2:E} )&amp;quot;, x, y, z); case &amp;quot;IJK&amp;quot;: StringBuilder sb = new StringBuilder(x.ToString(), 30); sb.Append(&amp;quot; i + &amp;quot;); sb.Append(y.ToString()); sb.Append(&amp;quot; j + &amp;quot;); sb.Append(z.ToString()); sb.Append(&amp;quot; k&amp;quot;); return sb.ToString(); default: return ToString(); } } public double Norm() { return x*x + y*y + z*z; } public Vector(Vector rhs) { x = rhs.x; y = rhs.y; z = rhs.z; } public override string ToString() { return &amp;quot;( &amp;quot; + x + &amp;quot; , &amp;quot; + y + &amp;quot; , &amp;quot; + z + &amp;quot; )&amp;quot;; } public double this [uint i] { get { switch (i) { case 0: return x; case 1: return y; case 2: return z; default: throw new IndexOutOfRangeException( &amp;quot;Attempt to retrieve Vector element&amp;quot; + i) ; } } set { switch (i) { case 0: x = value; break; case 1: y = value; break; case 2: z = value; break; default: throw new IndexOutOfRangeException( &amp;quot;Attempt to set Vector element&amp;quot; + i) ; } } } //public static bool operator == (Vector lhs, Vector rhs) //{ // if (lhs.x == rhs.x &amp;&amp; lhs.y == rhs.y &amp;&amp; lhs.z == rhs.z) // return true; // return false; //} private const double Epsilon = 0.0000001; public static bool operator == (Vector lhs, Vector rhs) { if (Math.Abs(lhs.x - rhs.x) &lt; Epsilon &amp;&amp; Math.Abs(lhs.y - rhs.y) &lt; Epsilon &amp;&amp; Math.Abs(lhs.z - rhs.z) &lt; Epsilon ) return true; return false; } public static bool operator != (Vector lhs, Vector rhs) { return ! (lhs == rhs); } public static Vector operator + (Vector lhs, Vector rhs) { Vector result = new Vector(lhs); result.x += rhs.x; result.y += rhs.y; result.z += rhs.z; return result; } public static Vector operator * (double lhs, Vector rhs) { return new Vector(lhs*rhs.x, lhs*rhs.y, lhs*rhs.z); } public static Vector operator * (Vector lhs, double rhs) { return rhs*lhs; } public static double operator * (Vector lhs, Vector rhs) { return lhs.x*rhs.x + lhs.y+rhs.y + lhs.z*rhs.z; } } } /* Results: FormattableVector In IJK format, v1 is 1 i + 32 j + 5 k v2 is 845.4 i + 54.3 j + -7.8 k In default format, v1 is ( 1, 32, 5 ) v2 is ( 845.4, 54.3, -7.8 ) In VE format v1 is ( 1.000000E+000, 3.200000E+001, 5.000000E+000 ) v2 is ( 8.454000E+002, 5.430000E+001, -7.800000E+000 ) Norms are: v1 is || 1050 || v2 is || 717710.49 || */
  3. using System; using System.Text.RegularExpressions; namespace Najah.ILoveCsharp.RegularExpressionPlayaround { class MainEntryPoint { static void Main() { Find1(); Console.ReadLine(); } static void Find1() { const string text = @&amp;quot;XML has made a major impact in almost every aspect of software development. Designed as an open, extensible, self-describing language, it has become the standard for data and document delivery on the web. The panoply of XML-related technologies continues to develop at breakneck speed, to enable validation, navigation, transformation, linking, querying, description, and messaging of data.&amp;quot;; const string pattern = @&amp;quot;\\bn\\S*ion\\b&amp;quot;; MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.ExplicitCapture); WriteMatches(text, matches); } static void Find2() { const string text = @&amp;quot;XML has made a major impact in almost every aspect of software development. Designed as an open, extensible, self-describing language, it has become the standard for data and document delivery on the web. The panoply of XML-related technologies continues to develop at breakneck speed, to enable validation, navigation, transformation, linking, querying, description, and messaging of data.&amp;quot;; const string pattern = @&amp;quot;\\bn&amp;quot;; MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase); WriteMatches(text, matches); } static void WriteMatches(string text, MatchCollection matches) { Console.WriteLine(&amp;quot;Original text was: \\n\\n&amp;quot; + text + &amp;quot;\\n&amp;quot;); Console.WriteLine(&amp;quot;No. of matches: &amp;quot; + matches.Count); foreach (Match nextMatch in matches) { int index = nextMatch.Index; string result = nextMatch.ToString(); int charsBefore = (index &lt; 5) ? index : 5; int fromEnd = text.Length - index - result.Length; int charsAfter = (fromEnd &lt; 5) ? fromEnd : 5; int charsToDisplay = charsBefore + charsAfter + result.Length; Console.WriteLine(&amp;quot;Index: {0}, \\tString: {1}, \\t{2}&amp;quot;, index, result, text.Substring(index - charsBefore, charsToDisplay)); } } } }