473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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 2420
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: William Stacey [MVP] | last post by:
Would like help with a (I think) a common regex split example. Thanks for your example in advance. Cheers! Source Data Example: one "two three" four Optional, but would also like to...
2
by: Michael R. Pierotti | last post by:
Dim reg As New Regex("^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$") Dim m As Match = reg.Match(txtIPAddress.Text) If m.Success Then 'No need to do anything here Else MessageBox.Show("You need to enter a...
7
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not...
9
by: taylorjonl | last post by:
I am having a problem matching some text. It is a very simple pattern but it doesn't seem to work. Here goes. <td*>.*?</td> That is the pattern, it should match any <td></td> pair. Here is...
5
by: SMOlesen | last post by:
Hi I need to split a comma delimited text, however if the comma is between ' ' then no split should occur ie: Class.Value, 'true' , 'some text', 'false', 'text, text, text' should split...
8
by: sherifffruitfly | last post by:
Hi, I've been searching as best I can for this - coming up with little. I have a file that is full of lines fitting this pattern: (?<year>\d{4}),(?<amount>\d{6,7}) I'm likely to get a...
1
by: Prabhu Gurumurthy | last post by:
Hello all - I have a file which has IP address and subnet number and I use regex to extract the IP separately from subnet. pattern used for IP: \d{1,3}(\.\d{1,3}){3} pattern used for...
5
by: Bosconian | last post by:
I have a textarea form field for inputting (or pasting) pairs of data. I need a regular expression pattern to validate each line for the following double quote number double quote comma...
2
by: John B | last post by:
I am trying to do a pretty simple pattern match using regex. The pattern is ^(?:(?<Item>.*?)@:@)*$. This should return a match for test123@:@ but does not, instead it never returns when I call...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.