469,955 Members | 2,388 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,955 developers. It's quick & easy.

Can't put a comma in a regex pattern?

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

Mar 6 '07 #1
4 2263
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

--
http://barrkel.blogspot.com/
Mar 6 '07 #2
On Mar 6, 2:40 am, Barry Kelly <barry.j.ke...@gmail.comwrote:
>
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

Mar 6 '07 #3
sherifffruitfly wrote:
On Mar 6, 2:40 am, Barry Kelly <barry.j.ke...@gmail.comwrote:
>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
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.

--
Göran Andersson
_____
http://www.guffa.com
Mar 6 '07 #4
On Mar 6, 8:24 am, Göran Andersson <g...@guffa.comwrote:
It doesn't cost anything to add a using line for System.Globalisation,
or to specify System.Globalisation.NumberStyles directly.
Ah - cool - thanks!

Mar 6 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by William Stacey [MVP] | last post: by
2 posts views Thread by Michael R. Pierotti | last post: by
7 posts views Thread by lgbjr | last post: by
9 posts views Thread by taylorjonl | last post: by
1 post views Thread by Prabhu Gurumurthy | last post: by
5 posts views Thread by Bosconian | last post: by
2 posts views Thread by John B | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.