Lior.Net


Archive for the ‘c#’ Category

Count words Using Regular Expression

Saturday, June 28th, 2008

 I had to create a program that counts words in a string. The program need to count all words in the string except:



1. Html Code

2. Dividers

3. Extra spaces + new lines 



I used regular expression to filter the strings. For this purpose I created two functions:

RemoveExtraSpaces - Remove extra spaces from string. The function allow only one space between each word.

CountWords - The function return number of words in a string. It use the function RemoveExtraSpaces and use regular expression to remove HTML Code , New Lines and Dividers.

/// <summary>



/// This function remove Extra spaces , the Regular expression is  

    looking for white spaces that appears 2 times and more

/// </summary>

/// <param name=”s”>This is the string that we want to check</param>

/// <returns>Fixed String</returns>

private string RemoveExtraSpaces(string s)

{

            Regex FindExtraSpace = new Regex(“\\s{2,}”);

            return FindExtraSpace.Replace(s, ” “);

}

/// <summary>

/// This function return the number of words in a string that are

/// separated by space

/// </summary>

/// <param name=”strText”>The text that we want to check</param>

/// <returns>number of words</returns>

public int CountWords(string strText)

{

    string exp = “#;#”;

   // The expression look for Html and new lines and the divider that  

   // we define before

   Regex Match = new Regex(“<[^>]+>|” + exp + “|\r\n|\n”);

   // Replace the tags with an empty string so they are not

   // considered in count

   strText = Match.Replace(strText, “”);

   // Remove the extra Spaces

   strText = RemoveExtraSpaces(strText);

   // Count the words in the string by splitting them wherever a

   // space is found

   return strText.Split(‘ ‘).Length;

 }

Regular Expression in C#

Wednesday, June 25th, 2008

In the following post I will explain what is regular expression, what we can do with it and the special characters that we can use in order to find the specific pattern.

The regular expression provide means to identify strings of text of interest. In other word the regular expression let us search a specific pattern in a text

What we can do with Regular Expression ?

1. Search easily specific pattern

2. Validate a string for example : credit card number, Id number and ect..

3. Find the specific pattern and do some action like replace characters

Example of regular expression in C#:

Private void regExample()

{       string str = “Hello. My name is Inigo Montoya. You killed my father prepare to die.”;

string exp = @”\binigo\b”;

Regex ex1 = new Regex(exp,RegexOptions.IgnoreCase);

// /b in the end and in the begining means that the word is Inigo and not part as other words like Inigojbkbj

// Moreover the option IgnoreCase mean that there is no meaning to the letters case.

Console.WriteLine(ex1.IsMatch(str));

// Match can give you more data where this string is located

Match match = ex1.Match(str);

Console.WriteLine(“Found string ‘” + match.Captures[0].Value + “‘ in:” + match.Captures[0].Index);

Console.WriteLine(ex1.Replace(str, “David”));

// Let’s find Inigo and afterward somewhere in the string should appear the word father

Regex ex2 = new Regex(@”\bInigo\b.*\bfather\b”);

match = ex2.Match(str);

foreach (Capture c in match.Captures)

{

  Console.WriteLine(“Found the pattern in:” + c.Value + ” in Index: “ + c.Index);

}

}

Special Characters that can help us search a pattern:

 

Character

 

Description

 

Example

 

/b

 

Matches at the position between a word character

 

/blior/b mean that the search will look for the word lior

 

|

 

Causes the regex engine to match either the part on the left side, or the part on the right side

 

Abc | def – check for abc or def

 

.

 

Match any single character

 

Ab. à

can be ab or abc , and and so on..

 

^

 

Matches at the start of the string

 

^ab – the string need to start with ab

 

[^]

 

Accept any character expect the characters in bracket

 

[^d-f] – expect all charcters d or e or f

 

$

 

Verify the end of the string

 

Def$ - check if the string end with def

 

*

 

Repeats the previous item zero or more times

 

a* - a,aa,aaa,aaaa,aaaaaaa

 

+

 

Repeat the previous item one or more times

 

a+ - aa,aaaa,aaaaaa

 

?

 

Zero or one of the preceding item

 

Dogs? -> dog, dogs

 

{n}

 

{n}

where n is an integer >= 1

 

B{2} = bb

 

{n,m}

 

{n,m}

where n >= 0 and m >= n

 

B{2,4} = bb,bbb,bbbb

 

{n,}

 

Repeat the previous item at least n times

 

D{2} = dd, dddd and so on.

 

/d

 

Any number

 

0-9

 

/w

 

Any number or letter

 

1,2,3,a,b,d,g and so on.

 

/s

 

Match any whitespace character

 

 

 

How To Work with Resources Files (Resx + resource) in C#

Thursday, June 19th, 2008

Many companies are working with resource files (.resx or .resource) in order to support multi-lingual Application \ Website. In the following Article I will demonstrate how to work with each kind.

According to the user culture, we can easily show our App \ Website in the proper language.

For Example:

// The following function return value of a key according to specific culture

Private string GetKeyValue(string filename, string resourcePath,string key,string culture)

{

ResourceManager rm = ResourceManager.CreateFileBasedResourceManager(resourceFile, filePath, null); Directory.GetCurrentDirectory(), null);

// retrieve the value of the specified key

String value = rm.GetString(key, CultureInfo.CreateSpecificCulture(culture));

if (!String.IsNullOrEmpty(value))

return value;

else

return null;

Resource Files

Read Resource File

private void ReadResourceFile(string filename)

{

// Read Entire File

IResourceReader reader = new ResourceReader(filename);

// Iterate through the resources and so some action

foreach (DictionaryEntry d in reader)

{

// Do Action

}

}

Write Resource File

private void WriteResourceFile(string filename)

{

IResourceWriter writer = new ResourceWriter(filename);

// Add Resources “Identifier” , value

writer.AddResource(”Identifier”, “value”);

writer.AddResource(”Identifier”, “value”);

// Close the file

writer.Close();

writer.Dispose();

Resx Files

Read Resx File
private void ReadResFile(string FileName)

{

ResXResourceReader rsxr = new ResXResourceReader(FileName);

// Iterate through the resources and do some action

foreach (DictionaryEntry d in rsxr)

{

// Do Action

}

}

Write Resx File

private void WriteResFile(string FileName)

{

ResXResourceWriter rsxr = new ResXResourceWriter(FileName);

// Add Resources

rsxr.AddResource(”Identifier”, “value”);

rsxr.AddResource(”Identifier”, “value”);

// Close the file

rsxr.Close();

rsxr.Dispose();

}

Jajah is the VoIP player that brought you web-activated telephony.