Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Regular Expressions
  4. What is the Regular expression that detects a $ sign and digits in html code

What is the Regular expression that detects a $ sign and digits in html code

Scheduled Pinned Locked Moved Regular Expressions
tutorialhtmlregexquestionlearning
7 Posts 4 Posters 18 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 8181754
    wrote on last edited by
    #1

    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.

    P J 2 Replies Last reply
    0
    • U User 8181754

      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.

      P Offline
      P Offline
      Paladin2000
      wrote on last edited by
      #2

      \$(\d{1,3}(,\d{3})*)(\.\d\d)?

      modified on Wednesday, September 7, 2011 10:24 AM

      U 1 Reply Last reply
      0
      • P Paladin2000

        \$(\d{1,3}(,\d{3})*)(\.\d\d)?

        modified on Wednesday, September 7, 2011 10:24 AM

        U Offline
        U Offline
        User 8181754
        wrote on last edited by
        #3

        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.

        P P 2 Replies Last reply
        0
        • U User 8181754

          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.

          P Offline
          P Offline
          Paladin2000
          wrote on last edited by
          #4

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

          Of 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

          U 1 Reply Last reply
          0
          • U User 8181754

            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.

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • U User 8181754

              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.

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • P Paladin2000

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

                Of 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

                U Offline
                U Offline
                User 8181754
                wrote on last edited by
                #7

                Thank you for the sample and the link. It works.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups