Regular expressions in C#

D

Dilip

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?
 
D

Dilip

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.
 
J

jp2msft

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 said:
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.
 
G

Göran Andersson

Dilip said:
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 $.
 
A

Arne Vajhøj

jp2msft said:
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
 
D

Dilip

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})?)$

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Regular Expressions Question 8
Regular expressions 3
Regex in C# 4
C# Regular Expressions 2
regular expressions 1
Regular expressions 3
regular expressions help 10
need some finer tuning on the regular expression 9

Top