I was working on a screen scrapping project, and was looking for away to automate the login form for an ASP.NET web page. Googling around, I found a very helpful article written by Scott Allen that lists the steps necessary to automate login using HttpWebRequest.
The first step of automating the login programmatically involved scrapping the hidden Viewstate field sent back to the client from the login page response. The article above listed the following Non-RegEx method to scrape:
private
string ExtractViewState(string s)
{
string viewStateNameDelimiter = "__VIEWSTATE";
string valueDelimiter = "value=\"";
int viewStateNamePosition =
s.IndexOf(viewStateNameDelimiter);
int viewStateValuePosition = s.IndexOf(
valueDelimiter, viewStateNamePosition
);
int viewStateStartPosition =
viewStateValuePosition + valueDelimiter.Length;
int viewStateEndPosition =
s.IndexOf("\"", viewStateStartPosition);
return
HttpUtility.UrlEncodeUnicode(
s.Substring(viewStateStartPosition,
viewStateEndPosition - viewStateStartPosition));
}
The above code can be simply replaced by a one line using the following Regular Expression:
"(?<=__VIEWSTATE\" value=\")(?<val>.*?)(?=\")"
So the above ExtractViewState function would turn into a one liner like this:
private
string
ExtractViewState(
string
s)
{
return
HttpUtility
.UrlEncodeUnicode(
Regex
.Match(s,
"(?<=__VIEWSTATE\" value=\")(?<val>.*?)(?=\")"
)
.Groups[
"val"
].Value);
}
ahhh... the power of RegEx!