Can't put a comma in a regex pattern?

  • Thread starter Thread starter sherifffruitfly
  • Start date Start date
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
 
sherifffruitfly wrote:

ret = Convert.ToInt32(strVisits);
Total ........................ 1,277
(comma-separated) 1,000s, a System.FormatException is thrown, telling
me that the input string is not in the correct format.

Convert.ToInt32(string) (Int32.Parse under the covers, IIRC) does not
understand commas in its strings. (They are culturally sensitive, BTW -
',' could be a decimal separator.) Try:

ret = Int32.Parse(strVisits, NumberStyles.AllowThousands);

You'll have to use System.Globalization to get NumberStyles.

-- Barry
 
Convert.ToInt32(string) (Int32.Parse under the covers, IIRC) does not
understand commas in its strings. (They are culturally sensitive, BTW -
',' could be a decimal separator.) Try:

ret = Int32.Parse(strVisits, NumberStyles.AllowThousands);

(smacks forehead) I was too caught up in verifying the regex to
consider other possibilities - thanks!

Solving the problem at the other end (avoid including another
namespace):

strVisits = strVisits.Replace(",","");


Thanks again!

cdj
 
sherifffruitfly said:
(smacks forehead) I was too caught up in verifying the regex to
consider other possibilities - thanks!

Solving the problem at the other end (avoid including another
namespace):

strVisits = strVisits.Replace(",","");


Thanks again!

cdj

Note: The System.Globalisation namespace is already available, it's part
of the mscorlib.dll. Globalisation is used for every conversion that is
culture dependant.

It doesn't cost anything to add a using line for System.Globalisation,
or to specify System.Globalisation.NumberStyles directly.
 
It doesn't cost anything to add a using line for System.Globalisation,
or to specify System.Globalisation.NumberStyles directly.

Ah - cool - thanks!
 
Back
Top