I'm looking to pull pricing information from a web site. To do this, I use a regex to find all instances where the first "$" is located. From there I use substring to grab the next 7 characters, which will be e.g. $42,945. I remove all the text before the "$" and repeat the process multiple times for the different $ amount values located on the website that I am using via For
loop.
The problem I have is after I trim the string to then go to the next $, the original string is recreated.
Here is the code that I am using:
WebClient client = new WebClient();
string allcontent = client.DownloadString("example.com");
string body = allcontent.Substring(140480,200000);
Regex rx = new Regex("[$]");
var numberCount = rx.Matches(body).Count;
string price = String.Empty;
string price2 = String.Empty;
int match = Int32.MaxValue;
string trimmed = String.Empty;
List<string> priceList = new List<string>();
for (int i = 0; i < numberCount; i++)
{
trimmed = body;
match = rx.Match(trimmed).Index;
price = trimmed.Substring(match, 7);
priceList.Add(price);
trimmed = trimmed.Remove(0, match + 7);
}
Console.WriteLine(priceList[0]);
Console.WriteLine(priceList[1]);
Console.ReadKey();
Suppose the string is: ABC $300 DEF $600 GHI $120 JKF $980
After the first loop iteration I should get $300
, on the second $600
, and so on. Instead I am getting $300
every time.
How can I fix this to get the correct values?