Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
DotNet
acad.assignment.mngr
academic
adonet
aspnet
aspnet.announcements
aspnet.build.controls
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
clr
compactframework
component_services
datatools
distributed_apps
drawing
faqs
framework
framework.wmi
general
internationalization
interop
languages.csharp
languages.jscript
languages.vb
languages.vb.controls
languages.vb.data
languages.vb.upgrade
languages.vc
languages.vc.libraries
myservices
odbcnet
performance
remoting
scripting
sdk
security
setup
vjsharp
vsa
webservi.enhancements
webservices
windowsforms
windowsforms.controls
winforms.databinding
winforms.designtime
xml
  
 
date: Tue, 22 Jul 2008 10:32:53 -0700 (PDT),    group: microsoft.public.dotnet.languages.csharp        back       


Regular expressions in C#   
I am using the .NET regular expressions library to match anything that
resembles a price/amount (33.33, 225.44 etc) out of a stream.  I have
this pattern with me currently:

\d+(\.\d*)?

This doesn't match the string as one full word.  IOW, I want the regex
to match a dollar amount in its entirety (any number of digits before
the decimal point but only 2 digits after the decimal).  I am a total
newbie to Regex and the best I came up with matches even things like
33a.67b (i.e it matches the 33 and 67 in that string whereas I want to
ignore that case completely).

Can someone help?
date: Tue, 22 Jul 2008 10:32:53 -0700 (PDT)   author:   Dilip

Re: Regular expressions in C#   
On Jul 22, 12:32 pm, Dilip  wrote:
> I am using the .NET regular expressions library to match anything that
> resembles a price/amount (33.33, 225.44 etc) out of a stream.  I have
> this pattern with me currently:
>
> \d(\.\d*)?

Ok, so this is the best I have come up with:

(\d[,.]\d\d?)

Any downsides to this?  I used the ',' because not all international
currencies use '.' as the decimal point to represent price.
date: Tue, 22 Jul 2008 10:56:51 -0700 (PDT)   author:   Dilip

Re: Regular expressions in C#   
I found this in the Help:

// Define a regular expression for currency values.
Regex rx = new Regex(@"^-?\d+(\.\d{2})?$");

I don't know what it means, though. It looks like jibberish! :)

"Dilip" wrote:

> On Jul 22, 12:32 pm, Dilip  wrote:
> > I am using the .NET regular expressions library to match anything that
> > resembles a price/amount (33.33, 225.44 etc) out of a stream.  I have
> > this pattern with me currently:
> >
> > \d+(\.\d*)?
> 
> Ok, so this is the best I have come up with:
> 
> (\d+[,+.]\d+\d+?)
> 
> Any downsides to this?  I used the ',' because not all international
> currencies use '.' as the decimal point to represent price.
>
date: Tue, 22 Jul 2008 13:15:27 -0700   author:   jp2msft

Re: Regular expressions in C#   
Dilip wrote:
> On Jul 22, 12:32 pm, Dilip  wrote:
>> I am using the .NET regular expressions library to match anything that
>> resembles a price/amount (33.33, 225.44 etc) out of a stream.  I have
>> this pattern with me currently:
>>
>> \d+(\.\d*)?
> 
> Ok, so this is the best I have come up with:
> 
> (\d+[,+.]\d+\d+?)
> 
> Any downsides to this?

Yes, it would also match strings like:

1+00
1.00000000000000
1+00000000000

> I used the ',' because not all international
> currencies use '.' as the decimal point to represent price.

You will have some better luck with:

^(\d+(?:[,.]\d{2})?)$

^ = start of string
\d+ = at least one digit
(?: = non-catching parenthesis
[,.] = comma or period
\d{2} = exactly two digits
)? = content of parenthesis is optional
$ = end of string

This will match strings like:

1
1.23
1,23
1234
1234.56
1234,56

If you are not matching a complete string, but searching for occurances 
within a string, just remove ^ and $.

-- 
Göran Andersson
_____
http://www.guffa.com
date: Tue, 22 Jul 2008 23:22:59 +0200   author:   Göran Andersson

Re: Regular expressions in C#   
jp2msft wrote:
> I found this in the Help:
> 
> // Define a regular expression for currency values.
> Regex rx = new Regex(@"^-?\d+(\.\d{2})?$");
> 
> I don't know what it means, though. It looks like jibberish! :)

^             start
-?            optional sign
\d+           one or more digits
(\.\d{2})?    optional a period followed by two digits
$             end

Arne
date: Tue, 22 Jul 2008 17:40:56 -0400   author:   Arne Vajhøj

Re: Regular expressions in C#   
On Jul 22, 4:22 pm, Göran Andersson  wrote:
> Dilip wrote:
> > On Jul 22, 12:32 pm, Dilip  wrote:
> >> I am using the .NET regular expressions library to match anything that
> >> resembles a price/amount (33.33, 225.44 etc) out of a stream.  I have
> >> this pattern with me currently:
>
> >> \d(\.\d*)?
>
> > Ok, so this is the best I have come up with:
>
> > (\d[,.]\d\d?)
>
> > Any downsides to this?
>
> Yes, it would also match strings like:
>
> 1
> 1.00000000000000
> 1퍍㓓䴴퍍
>
> > I used the ',' because not all international
> > currencies use '.' as the decimal point to represent price.
>
> You will have some better luck with:
>
> ^(\d(?:[,.]\d{2})?)$

Goran
Thank you very much.  This was very helpful.  I don't use regular
expressions a whole lot in my line of work and didnt' want to read an
entire book to match a trivial pattern like this.
date: Wed, 23 Jul 2008 08:15:35 -0700 (PDT)   author:   Dilip

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us