What is the Regular expression that detects a $ sign and digits in html code
-
Hi Everyone, I am trying to figure out how to parse html line and take from it the $ sign and a the monetary value. For example: The price of the book is $15.00 and the price of the computer is $1,299.99 cents. Thank you.
-
Hi Everyone, I am trying to figure out how to parse html line and take from it the $ sign and a the monetary value. For example: The price of the book is $15.00 and the price of the computer is $1,299.99 cents. Thank you.
\$(\d{1,3}(,\d{3})*)(\.\d\d)?
modified on Wednesday, September 7, 2011 10:24 AM
-
\$(\d{1,3}(,\d{3})*)(\.\d\d)?
modified on Wednesday, September 7, 2011 10:24 AM
Thanks for the reply. I doesn't seem to work. Can you explain briefly what you are attempting to match with that expression? Thank you.
-
Thanks for the reply. I doesn't seem to work. Can you explain briefly what you are attempting to match with that expression? Thank you.
It does work. Try this in a console app:
static void Main(string[] args)
{
Regex rx = new Regex(@"\$(\d{1,3}(,\d{3})*)(\.\d\d)?");
foreach (Match m in rx.Matches("This matches $4.12 dollars. It also matches $32.32 or $15 or $2.11 or $0.12 or $156,789.33 or $12,345.67"))
{
Console.WriteLine(m.Value);
}
Console.ReadKey();
}Output:
$4.12
$32.32
$15
$2.11
$0.12
$156,789.33
$12,345.67Of course, you must reference the System.Text.RegularExpressions namespace. Also, here is a page you can use to help learn regular expression syntax: http://www.regular-expressions.info/reference.html
-
Thanks for the reply. I doesn't seem to work. Can you explain briefly what you are attempting to match with that expression? Thank you.
I ran that Regex against your test case and it works perfectly. What output are you getting? What does your code look like that's calling it?
Forgive your enemies - it messes with their heads
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Hi Everyone, I am trying to figure out how to parse html line and take from it the $ sign and a the monetary value. For example: The price of the book is $15.00 and the price of the computer is $1,299.99 cents. Thank you.
Member 8216828 wrote:
I am trying to figure out how to parse html line and take from it the $ sign and a the monetary value.
In general it isn't a good idea to parse html/xml via regular expressions. Instead one should use a html/xml parser. One might use regex if all of the following is true. 1. The source(s) for html/xml are limited. Thus for example there is only one source and there is unlikely to be another. 2. The source is machine generated (thus a library was used to create it rather than an adhoc code of some sort.) The reason this isn't a good idea is because there are many variations in the way html/xml can be formatted. By the time one has accounted for all of those variances either one has a parser or one has a maintenance problem (or maintenance nightmare).
-
It does work. Try this in a console app:
static void Main(string[] args)
{
Regex rx = new Regex(@"\$(\d{1,3}(,\d{3})*)(\.\d\d)?");
foreach (Match m in rx.Matches("This matches $4.12 dollars. It also matches $32.32 or $15 or $2.11 or $0.12 or $156,789.33 or $12,345.67"))
{
Console.WriteLine(m.Value);
}
Console.ReadKey();
}Output:
$4.12
$32.32
$15
$2.11
$0.12
$156,789.33
$12,345.67Of course, you must reference the System.Text.RegularExpressions namespace. Also, here is a page you can use to help learn regular expression syntax: http://www.regular-expressions.info/reference.html
Thank you for the sample and the link. It works.