myfavoritemovies.us

April 24, 2008

Finding a pattern in a string

Filed under: regex,Tips and Tricks — Tags: , — @ 7:52 pm

It is easier to use Regex to find the string pattern you are looking for in a string using C#. If you are not sure how to use them, please use this link.

If you are trying to find a string with control characters please remeber to use ‘\’ character in front of it. Most of the time, you may not be looking for a string with special characters in it. If you are then make user you use correct character.

For example, I have a string x = “$\John\Mary”

I need to replace $\John\ with say blank, I want only Mary from that string. Then you need to do the following

string result = Regex.Replace(x, @”/$/\John/\”, “”);

With this each control character is preceded by / so that, regex will use that as a normal character otherwise, regex will try to interpret them as special character and you will not find the match.

Powered by WordPress