-
-
Regular Expressions And Extracting Strings
by azamsharp on 6/13/2008 10:47:12 AM
-
Recently, I had to extract some text (keycode) out of a large text. So, I decided to use regular expression. The text to be extracted was in the form of => Problem Code: 3234. The 4 digit number can be anything. Here is the code that is used to extract the keycode out of the large text.
-
public class KeyCodeService
{
private static string pattern = @"Problem Code: \s?(\d+)*";
private static Regex regex = new Regex(pattern, RegexOptions.Compiled);
public static string ExtractKeyCode(string text)
{
string keyCode = String.Empty;
Match match = regex.Match(text);
if (match.Groups.Count > 1)
{
keyCode = match.Groups[1].Value;
}
return keyCode;
}
}
-
Refactor it!
Please log in to refactor the code!
Login