|
|
|
date: Tue, 22 Jul 2008 10:32:53 -0700 (PDT),
group: microsoft.public.dotnet.languages.csharp
back
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#
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
|
|