Monday, April 02, 2018

Regular Expressions with C# Interactive

Yes, you can. However you have to reference the Regex class (System.Text.RegularExpressions) as shown in this image.

After this you can use its objects and you have intellisense to provide you help in coding.

The Syntax for using RegularExpressions is:

Match match = Regex.Match(InputStr, Pattern, RegexOptions)

You set up a pattern and you test your Pattern against the InputStr you set up to see if there is a match.

Let me shown a very simple example (the example is run in Interactive C# in Visual Studio Community 2017). Make sure you have already used using System.Text.RegularExpressions before executing these statements:

In the above, the InputStr is 'Jayaram' and the Pattern is shown above. You need to type each line and hit Enter. It is somewhat slow.
In the above case, a match was found for the first lower-case letter in the InputStr.

Here is another example:

The match is for the first upper-case letter in the string.

Here is an example for 2 adjacent numbers in the string:

Only one of the blocks there is a match. Same pattern but different InputStr.

No comments: Protection Status