S
sherifffruitfly
Hi all,
I can't see what's wrong with this regex pattern:
private int ParsePageViews(string str)
{
int ret = 0;
string pattern = @"Visits.*\n\s*Total\s\.*\s(?
<visits>(\d{3})|(\d,\d{3}))";
if (Regex.IsMatch(str, pattern))
{
Match m = Regex.Matches(str, pattern)[0];
string strVisits = m.Groups["visits"].Value;
ret = Convert.ToInt32(strVisits);
}
return ret;
}
Error-generating sample input (newlines didn't translate
appropriately):
" -- Site Summary ---
Visits
Total ........................ 1,277
Average per Day .................. 3
Average Visit Length .......... 5:45
This Week ....................... 20
Page Views
Total ........................ 1,757
Average per Day .................. 4
Average per Visit .............. 1.5
This Week ....................... 29"
I'm parsing a bunch of strings of this sort. When the page visits are
in the 100s, everything is fine. When the page visits are in the
(comma-separated) 1,000s, a System.FormatException is thrown, telling
me that the input string is not in the correct format.
When I take the regex pattern's comma out of the pattern, the
exception goes away - but of course, then I don't match what I want
to.
The pattern works just fine in Expresso. What am I missing here?
Thanks for any ideas,
cdj
I can't see what's wrong with this regex pattern:
private int ParsePageViews(string str)
{
int ret = 0;
string pattern = @"Visits.*\n\s*Total\s\.*\s(?
<visits>(\d{3})|(\d,\d{3}))";
if (Regex.IsMatch(str, pattern))
{
Match m = Regex.Matches(str, pattern)[0];
string strVisits = m.Groups["visits"].Value;
ret = Convert.ToInt32(strVisits);
}
return ret;
}
Error-generating sample input (newlines didn't translate
appropriately):
" -- Site Summary ---
Visits
Total ........................ 1,277
Average per Day .................. 3
Average Visit Length .......... 5:45
This Week ....................... 20
Page Views
Total ........................ 1,757
Average per Day .................. 4
Average per Visit .............. 1.5
This Week ....................... 29"
I'm parsing a bunch of strings of this sort. When the page visits are
in the 100s, everything is fine. When the page visits are in the
(comma-separated) 1,000s, a System.FormatException is thrown, telling
me that the input string is not in the correct format.
When I take the regex pattern's comma out of the pattern, the
exception goes away - but of course, then I don't match what I want
to.
The pattern works just fine in Expresso. What am I missing here?
Thanks for any ideas,
cdj