TopMenu

Learn Regular expressions easy way with Visual Studio

These days having knowledge of Regular expressions can stand you out of crowd of developers. With everyday’s work it’s really hard to get time to learn everything. But Regular Expression are something that developers use for their day to day work. Those who are new or are not familiar with RegEx(Regular Expressions) are usually depends on searching for required regex on internet e.g. StackOverflow and other sites. Most important this is some skill which can be used across platforms.

In this post you’ll learn how to create regular expression by yourself and how to start practicing while working with Visual Studio. Considering you’re working on a large codebase and regularly you need to search for method names, Event names or just some code lines or anything that you want to look up in the code base. Usually what developers do use is “Find in Files” or “Find All” with given terms. But what if you don’t know the exact name of the code line you want to search. What will you do? Well, you can definitely think of a patterns that may help you find your target line of code.

I have this EntityFramework 6 code base forked from Git repository. This is a pretty large code base to look into something. So For e.g. I want to look for all classes used in validation. I’ll start my search with following in mind:
- Assuming they’re named properly with the prefix, suffix or in between the name of a classes.
- The classes names may contain variations like “Validator”, “Validation”, Validatable” etc.

Either I can search for all these terms individually and collect results manually, Or I can use pattern matching and find all result in one go. So I’m going to open the “Find in files” window(SHIFT + CTRL + F) and start writing a Regular expression for it:
image
I’m using the expression .* class \w+Valida\w+ (I’ll explain next what it means) to find a patterns based on my assumptions above. After clicking on “Find All” I have following results:
image
As you can see all my desired classes are listed in the results “Viola!!”.
Instead of searching for individual keywords I have my result in single search. I saved time and I have my regular expression which might not be perfect and it did the job.

Explanation of Regex used aboveLet break down this and see what individual things means:
. – Represents anything(character-non character, symbols, digits etc.) in a string.
* – Represents 0 or more occurrences.
class – Represents a string literal and it will be an exact match.
\w – Represents any alphanumeric and including _ (underscore).
+ – Represents 1 or more occurrences.
Valida - Represents a string literal and it will be an exact match
\w+ – As explained above.
Well, this was easy or is it not?

Another simplest case I can mention here which every developer might have faced is managing Spaces in Code, in Text or in Strings at run time. A simple way to remove additional spaces using regular expression is, (I’m using C# Regex library for this demo):

Regex.Replace(“  Some Text     with un      even spaces”, “(\s+)”, “(\s)”).Trim();
Output -
“Some Text with un even spaces”

Let’s go back a little and start with basics and then go step by step:
Step 1 – If you are completely new to the Regular Expressions then you can start with a free practice website which provide insights and great details of each exercise https://regexone.com
Explanation of symbols are provided in the right side column. But I’m also going to use the screenshot from the website:
image
This will give you pretty good explanation of each special character and symbols used in Regualar Expressions.
After memorizing each and every of these notation you can start taking the exercises one by one. And in hardly 1-2 hours of practice you will be able to write you first Regular Expression without any help.
Another source to learn and test my regex I prefer to https://regex101.com
This site provide better insights of your regular expression on the fly. But I would say once you are on the next level and have confident go the website and challenge your Regex with various tests and insights including optimization of your Regular Expression.







Step 2 – Now that you have learned to write the basic regular expression. It’s time to test your skills in visual studio. As I stated an example above you can start using Regular expression as search option instead of searching for “Contains” search in visual studio. And at this step you are already stepped into the Advanced user of visual studio.
Step 3 – Creating complex expression and getting help right from visual studio. If you don’t know then Let me show you that visual studio have it’s built-in helper utility to create regular expression right from visual studio.
Open the “Find in Files” dialog window, Select “Regular Expression” Like a boss.
image

Then click on button next to the “Find What:” input box. And you’ll see the hidden treasure:
image
These are the tricks at your finger tips. Now you can start writing and testing your regex using “Quick Find” as well. It shows you the live match results by highlighting the matching result with in the Editor window.
So for e.g. I wan to search anything that start with Collection then here’s my quick tip, Just go to “Quick Find’'” (CTRL + F) and write your regex:
image
image
With the combination of search and replace you can easily refactor your code without repeating steps and saving a lot of time for other stuff.

Testing your Random Regular Expression:
You can again use the Visual studio editor to test your regex which you might be going to use in your code. How? Simply create a text file with your sample Text data. E.g. you want to capture cell phone from a Text,
/* == Matching cell phone numbers == */
3473459090
12123232111
ABdfasdfasdlf
Another test string
1106 is a zip code
but this is a cell phone number +1 121 212 2241
121 212 2241

Now open the quick find box and put your Regular Expression in there and see what is matching and what is not.
For e.g. I prepared a Regex to find the phone numbers as 1?[\s-]?\(?(\d{3})\)?[\s-]?\d{3}[\s-]?\d{4}
Let’s see how my results are looking:

 image
Now instead of going to some online website to test your regex you can directly give a dry run with in Visual studio. Hope you have had fun reading and learning one of the essential skills of quick smart developer which can be you. Keep practicing!!

Hands on CSharp 7.0 features in VS 2017 RC

As of now me and you might have seen a lot of blog posts, articles, StackOverflow Documentation and from many other sources about the new/proposed features of C# 7.0 since the announcement in Aug 2016. I’m not going to write about the same as some are finalized but more these features are still under discussion with C# design teams.
In this blogs post I’m going to address how to start hands on with new C# 7.0 feature rolled out with Preview release.
If you have VS2017 RC installed on you machine then make sure you have installed the .Net core and Asp.net core tooling. If not you can again launch the setup and choose those module to install.
Now to quickly get started You can download the sample from Code.Msdn.Microsoft.com. This sample have all the feature well documented and a sample project which you can start with for hands on excercise.
Now since I have this project downloaded so when I’m opening it with VS2017 RC and trying to build it getting error:

image
To fix it you can open the Nuget package and try to get the latest Stable version i.e. 4.3.0 at this moment.

image

After update try to build again. Unfortunately I wasn’t able to build it on my machine and got below error similar to the previous one.
image

Now in order to get things working go to New Project in the same solution and Create a new .Net core console app.
SNAGHTML16e24a79

Restore the Nuget package for System.ValueTuple v 4.3.0 version in the newly created project:
image
Now after installing the package go to the Program.cs file which isn’t in the project so If you try to add it in the ConsoleApp it will throw an error that file with same name already exist.
noProgramCSFile
But it’s not there, Possibly hidden. So you can click on top corner option of SolutionExplorer to show Hidden files.
HiddenProgramCS
Now Right click on the Program.cs and select Include the file in the Project. Now that you have the consoleApp ready and Nuget package installed. Now it’s time to add those sample from the solution we downloaded from Code MSDN and get the file in the new project.
Right Click on the Project and Add –> Existing Items… Navigate to the directory where the solution directory is and open the Samples folder, Select all files and “Add as Link”.
image
After adding the files now open the Program.cs file and copy the content of Program.cs from the sample project or copy below code in your Program.cs

using CS70SampleConsole;
using System;

class Program
{
    static void Main(string[] args)
    {
        OutVariablesSample.Run();
        PatternMatchSample.Run();
        TupleSample.Run();
        DeconstructionSample.Run();
        LocalFunctionsSample.Run();
        DigitSeperatorSample.Run();
        RefReturnsAndLocalsSample.Run();

        Console.Read();
    }
}
Build the new .Net core console App and run by pressing ‘F5’ and VIOLA!!
SNAGHTML16ed313e
Now you can start playing around the new features. Documented in the CodeSample as they are the only features delivered as Preview.

Arrays in F# – The Mutable collection

Arrays in F# are mutable data type. I started intentionally with this line because FSharp(F#) is a functional programming language and the paradigm of language promotes immutability. So Whenever there is a mutable type or value be careful with the usage. Let’s go back to main topic i.e. Arrays.

In this post we’ll see how to initialize, iterate, update and other manipulation that you can do with Arrays in FSharp(F#). In this post we’ll cover only one dimensional array. May be we can discuss 2D, 3D, and Jagged array in advanced array post of arrays in future.

Let’s have fun with playing around Arrays.

Static array initialization

In FSharp(F#) we initialize the array with following notations:

let fruits = [|"Apple"; "Orange"; "Grapes"|]

// or using every item in separate line without separator

let fruits = [|

        "Apple"

        "Orange"

        "Grapes"

    |]

Notice additional symbol | (pipe) used with square braces to declare the array. Also the separator is not , comma anymore but it’s ; semicolon. Array of integer sample and possible mistakes that can be done by a C# developer without getting any compile time error.

let arr = [| 1; 2; 3; 4 |]
 
Possible mistakes #1  
// most common mistakes done by developers with background of imperative language
let arr1 = [| 1, 2, 3|]
// this is an array of tuples with one element, so no error is displayed.
// the ',' separator is used to create tuples.
 
Possible mistakes #2 
// what if the | is not used in declaration
let arr2 = [1; 2; 3]
// this is also valid but this would be a list and it's "IMMUTABLE"

Initialization using sequence expressions

// Array of squares of positive integers from 1 to 10
let arr3 = [| for i in 1 .. 10 -> i * i |]
Output -  [|1; 4; 9; 16; 25; 36; 49; 64; 81; 100|]  

Initialization using Array module

Array.empty : creates a new array that does not contain any elements.

// Specify the type by using a type argument.
let array1 = Array.empty<int>
// Specify the type by using a type annotation.
let array2 : int array = Array.empty
 
// Even though array3 has a generic type,
// you can still use methods such as Length on it.
let array3 = Array.empty
printfn "Length of empty array: %d" array3.Length

Output - 0

Array.create : creates an array of a specified size and sets all the elements to provided values.

// creates an array of length 10, all elements initialized with default value -1
let arrayOfTenZeroes : int array = Array.create 10 -1
// create a bool array with default value as true
let primeFlaggedArr : bool array = Array.create 10 true

Note – In the end of this article we’ll solve the find prime number problem using Sieve of Eratosthenes.

Array.zeroCreate : creates an array, given a dimension and a function to generate the elements.

// create a bool array with default values(false)
let primeFlaggedArr : bool array = Array.zeroCreate 10

Array.init : creates an array, given a dimension and a function to generate the elements.

// Another way to create array of squares starting from 1 to 10
printfn "Array of squares: %A" (Array.init 10 (fun index -> index * index))

Accessing array elements

You can access array elements by using a dot operator (.) and brackets ([ and ]).

let fruits = [|
        "Apple"
        "Orange"
        "Grapes"
    |]
 
let firstFruit = fruits.[0]

Accessing range of values from array using sliced notation

// Accesses elements from 0 to 2.
arr.[0..2]  
// esses elements from the beginning of the array to 2.
arr.[..2] 
// esses elements from 2 to the end of the array.
arr.[2..] 

Few more honorable mentions or generally used functions are Array.get, Array.set and Array.length which are self-explanatory.

Replacing existing value using index

Arrays are mutable so you can replace any existing value by accessing it via index and assigning a new value with ‘<-‘ operator.

// Updating the value of an item in array using index
let fruits1 = [|
        "Apple"
        "Orange"
        "Grapes"
    |]
 
fruits1.[0] <- "Mango"
 
let print = 
    for (fruit) in fruits1 do
        printfn "%s" fruit

 

Output –

> PrintUpper [|"Apple"; "Orange"; "Grapes"|] ;;

APPLE

ORANGE

GRAPES

Iterating through array elements

Let’s create a function that takes an array of string and convert the values to upper case.

let MakeUpper(arr : string array) =
    for i in 0 .. arr.Length - 1 do
        Array.set arr i (arr.[i].ToUpper())
        printfn "%s"  arr.[i]

In visual studio select the function and run it in F# interactive.

Output –

> MakeUpper [|"Apple"; "Orange"; "Grapes"|] ;;

APPLE

ORANGE

GRAPES

With variant of for loop (foreach alike)

let PrintUpper(arr : string array) =
    for (fruit) in arr do
        printfn "%s"  (fruit.ToUpper())

 

Output –

> PrintUpper [|"Apple"; "Orange"; "Grapes"|] ;;

APPLE

ORANGE

GRAPES

So now we are pretty much good to start with playing around array. Let’s try to solve few problems using array to get up to speed. As I discussed before we’ll be solving the finding prime number problem using Sieve of Eratosthenes. Here’s a function to which is based on theory given at source:

// print prime number in a series up to n 
let PrimeSeive(n : int) = 
    let sequence = [|1..n|]
    let isPrime = Array.create n true
    let maxSquareRoot : int = System.Convert.ToInt32(System.Math.Sqrt(float n))
    let candidate = 2;
    
    for i in candidate..maxSquareRoot do
        if isPrime.[i] then
            let mutable j = i * i
            while j < n - 1 do
                isPrime.[j] <- false
                j <- j + i
 
        
    for index in 0..n - 1 do
        if isPrime.[index] then
            printf "%d, " index

 

Output –

> PrimeSeive 100 ;;

0, 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 99, val it : unit = ()

Here’s the equivalent code in CSharp(C#)

public static void PrimeSeive(int n)

{

    bool[] isPrime = new bool[n + 1];

    int maxSquareRoot = (int)Math.Sqrt(n);

 

    int candidate = 2;

 

    // Additional loop to initialize the bool array with default value

    for (int ind = 0; ind < isPrime.Length; ind++)

    {

        isPrime[ind] = true;

    }

 

    for (int i = candidate; i < Math.Sqrt(n); i += 1)

    {

        if (isPrime[i])

        for (int j = i * i; j < n; j = j += i)

        {

            isPrime[j] = false;

        }

    }

 

    for (int k = 0; k < n; k++)

    {

        if(isPrime[k])

        Console.WriteLine(k);

    }

}

Although the given solution in F# is not the best and not recommended because we’re using much of mutable types like any other imperative language. I’m not sure you readers have notices but look at the F# snippet how beautiful it does look without any block defining braces. In next post we’ll solve the same problem with immutable types and without using mutable.

Here’s few exercises for you:

What would be the output of below code?

// Guess the output, Write your answer in the comments.
let guessOutput = 
        for i in [|9..7..30|] do
         printfn "%d" i

Happy Learning!!!!

string manipulations in F#

In previous posts we learned and got familiar with the theory and code of Functional programming language F#. The last post was mostly used operation of any language “The Print”. In this post we’ll use the mostly used type and literals in code i.e. String.
A string

String is an immutable data type in C# and F# is all about immutability. So you will not see much different between the C# string and F# string type. Means you’re already familiar with escape sequences\verbatim strings. Below is the list of supported escape sequences in F#:

Character

Escape sequence

Backspace

\b

Newline

\n

Carriage return

\r

Tab

\t

Backslash

\\

Quotation mark

\ "

Apostrophe

\'

Unicode character

\uXXXX or \UXXXXXXXX (where X indicates a hexadecimal digit)


The Verbatim string

Like in C# the verbatim string is ignore special character i.e. escape sequences. In F# it works with same symbol @ and special triple-quoted string.

// Using a verbatim string to represent xml node in string

let xmlFragment1 = @"<book author=""Milton, John"" title=""Paradise Lost"">"

 

// Using a triple-quoted string

let xmlFragment2 = """<book author="Milton, John" title="Paradise Lost">"""

Linebreaks

Consider below examples:

let str1 = "Hello
     sir"
let str2 = "Hello, \
     Mam"

The first Str1 is a string with new line character “Hello \n sir”. Linebreaks are actually produces the new line character in a string spanning across lines.

Str2 is a continuous string “Hello, Mam”. When a ‘\’ backslash character is the last character before the line breaks leading whitespaces are ignored and no line breaks will be considered.

String is array* of characters

Means you can access individual character with index like in an array*.

printfn "%c" str1.[1]

*The Array in F# will be covered in a dedicated post later.

Substrings

The substring can be extracted from any string by supplying range in Index operator.

printfn "%s" (str1.[0..2]) // Substring from index 0 to 2
printfn "%s" (str2.[3..5]) // Substring from index 3 to 5

String as ByteArray (Ascii string)

// "abc" interpreted as a Unicode string.
let str1 : string = "abc"
// "abc" interpreted as an ASCII byte array. 
let bytearray : byte[] = "abc"B 

String operator

Just like C#, the ‘+’ operator concatenates two strings.

let string1 = "Hello, " + "world"

String Formatter

In the previous post we learned about the format specifiers. The all format specifiers can be used for string formatting. The equivalent of ‘String.Format’ in F# is ‘sprintf’

let formattedString = sprintf "The first letter of alphabet in %s is %c" "english" 'a'

Let’s run in F# interactive –

> formattedString;;

val it : string = "The first letter of alphabet in english is a"

The above are the basics of strings type in F#. Now let’s move to the interesting topic i.e. manipulation of strings. The string function are defined as module ‘Core.String’ under FSharp.Core namespace.

Library functions for string Manipulation

The list of function is available at above mentioned MSDN link. But here we’ll try to play a little with those function to understand what those functions actually do.

collect : (char -> string) -> string -> string

concat : string -> seq<string> -> string

exists : (char -> bool) -> string -> bool

forall : (char -> bool) -> string -> bool

init : int -> (int -> string) -> string

iter : (char -> unit) -> string -> unit

iteri : (int -> char -> unit) -> string -> unit

length : string -> int

map : (char -> char) -> string -> string

mapi : (int -> char -> char) -> string -> string

replicate : int -> string -> string

collect

Builds a new string whose characters are the results of applying a specified function to each of the characters of the input string and concatenating the resulting strings.

let collectTesting inputS =

    String.collect (fun c -> sprintf "%c# " c) inputS

  

printfn "%s" (collectTesting "CFJ")

Output –

C# F# J#

concat

Returns a new string made by concatenating the given strings with a separator.

let arr = [| "Sunday"; "Monday"; "Tuesday"; |]

 

let concatPlay = String.concat " comes after " arr

Output –

val concatPlay : string = "Sunday comes after Monday comes after Tuesday"

exists

Tests if any character of the string satisfies the given predicate.

let containsUppercase string1 =
    if (String.exists (fun c -> System.Char.IsUpper(c)) string1) then
        printfn "The string \"%s\" contains uppercase characters." string1
    else
        printfn "The string \"%s\" does not contain uppercase characters." string1
containsUppercase "Hello World!"
containsUppercase "no"

 

Output –

The string "Hello World!" contains uppercase characters.
The string "no" does not contain uppercase characters.

forall

Tests if all characters in the string satisfy the given predicate.

let isValidName string1 =

    if (String.forall (fun c -> System.Char.IsLetter(c)) string1) then

        printfn "The string \"%s\" is a valid name." string1

    else

        printfn "The string \"%s\" is not a valid name." string1

isValidName "Amit"

isValidName "Amit25"

 

Output –

The string "Amit" is a valid name.

The string "Amit25" is not a valid name.

init

Creates a new string whose characters are the results of applying a specified function to each index and concatenating the resulting strings.

let string1 = String.init 10 (fun i -> i.ToString())
printfn "%s" string1
let string2 = String.init 26 (fun i ->
    sprintf "%c" (char (i + int 'A')))
printfn "%s" string2

 

Output –

0123456789
ABCDEFGHIJKLMNOPQRSTUVWXYZ

iter

Applies a specified function to each character in a string.

let printCharacters sourceString =

    String.iter(fun c -> printfn "%c" c) sourceString

printCharacters "Happy New Year!!"

output –

H

a

p

p

y

 

N

e

w

 

Y

e

a

r

!

!

Similarly there are below function which takes lambda expressions(anonymous function) and applies on each character of given input.

map - Creates a new string whose characters are the results of applying a specified function to each of the characters of the input string.

iteri and mapi

iteri function applies a specified function to the index of each character in the string and the character itself and returns unit.

mapi function creates a new string whose characters are the results of applying a specified function to each character and index of the input string.

e.g.

let enumerateCharacters inputString = 
    String.iteri (fun i c -> printfn "%d %c" i c) inputString
enumerateCharacters "TIME"
enumerateCharacters "SPACE"

 

Output –

Index       Unit

0       T
1       I
2       M
3       E
 
0       S
1       P
2       A
3       C
4       E

replicate

Returns a string by concatenating a specified number of instances of a string.

printfn "%s" <| String.replicate 10 "<>"

Output –

<><><><><><><><><><> 

length

Returns the length of the string.

The sample included with each string library function are simplified as it’s a beginner level post. In upcoming posts we’ll see more complex examples with function piping, pattern matching etc.