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. Capture values using regex

Capture values using regex

Scheduled Pinned Locked Moved Regular Expressions
regexiothelpquestionlearning
4 Posts 2 Posters 10 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.
  • A Offline
    A Offline
    akwillows
    wrote on last edited by
    #1

    Hi I'm a Regex novice (very much learning as I go!) and I am trying to write a regex expression to capture the minimum and maximum temperature values in the string below. I'm hoping someone may be able to help e see where I have gone wrong?

    class="outsideimage m0200006341e8" title="">

                                Humidity
                                100%
                            
                            
                                Temp Min
                                3.1°C
                            
                            
                                Temp Max
                                5.7°C
    

    I have to include the m00006341e8 in the string to match as there are several other sensors that report in the overall text string and they all have a different serial number. I need to just extract the max and min values for this particular sensor. I think I've allowed for the fact that the humidity value may change I have tried the following, but it doesn't seem to be working: For the minimum temperature:

    class="outsideimage m0200006341e8" title="">Humidity[0-9]*\%Temp Min([-]?[0-9]*[.]?[0-9]?)

    and for the maximum temperature:

    class="outsideimage m0200006341e8" title="">Humidity[0-9]*\%Temp Min[-]?[0-9]*[.]?[0-9]?°CTemp Max([-]?[0-9]*[.]?[0-9]?)°C

    Richard DeemingR 1 Reply Last reply
    0
    • A akwillows

      Hi I'm a Regex novice (very much learning as I go!) and I am trying to write a regex expression to capture the minimum and maximum temperature values in the string below. I'm hoping someone may be able to help e see where I have gone wrong?

      class="outsideimage m0200006341e8" title="">

                                  Humidity
                                  100%
                              
                              
                                  Temp Min
                                  3.1°C
                              
                              
                                  Temp Max
                                  5.7°C
      

      I have to include the m00006341e8 in the string to match as there are several other sensors that report in the overall text string and they all have a different serial number. I need to just extract the max and min values for this particular sensor. I think I've allowed for the fact that the humidity value may change I have tried the following, but it doesn't seem to be working: For the minimum temperature:

      class="outsideimage m0200006341e8" title="">Humidity[0-9]*\%Temp Min([-]?[0-9]*[.]?[0-9]?)

      and for the maximum temperature:

      class="outsideimage m0200006341e8" title="">Humidity[0-9]*\%Temp Min[-]?[0-9]*[.]?[0-9]?°CTemp Max([-]?[0-9]*[.]?[0-9]?)°C

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Don't try to use regular expressions to parse HTML. Use a library which is designed for the job instead. For example, in .NET you should use either AngleSharp[^] or Html Agility Pack[^]. You may also need to look at the surrounding HTML - based on the fragment you've shown, it's not clear whether the data from the other sensors is sufficiently separated from the data you're trying to extract.


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      A 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        Don't try to use regular expressions to parse HTML. Use a library which is designed for the job instead. For example, in .NET you should use either AngleSharp[^] or Html Agility Pack[^]. You may also need to look at the surrounding HTML - based on the fragment you've shown, it's not clear whether the data from the other sensors is sufficiently separated from the data you're trying to extract.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        A Offline
        A Offline
        akwillows
        wrote on last edited by
        #3

        Thanks Richard. Unfortunately, in the software I am using, I don't have an alternative and have to use Regex since that is all that is supported. I've since realised that it IS capturing the minimum temperature correctly, but it is not returning the Maximum temperature for some reason.

        Richard DeemingR 1 Reply Last reply
        0
        • A akwillows

          Thanks Richard. Unfortunately, in the software I am using, I don't have an alternative and have to use Regex since that is all that is supported. I've since realised that it IS capturing the minimum temperature correctly, but it is not returning the Maximum temperature for some reason.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Check the raw source of the string you're trying to match. It could be that the ° character is actually °, °, or °. Or it could be a different Unicode character entirely - for example:

          • º = º / º / º
          • ˚ = ˚ / ˚ / ˚
          • ᵒ = ᵒ / ᵒ
          • ゜ = ゜ / ゜
          • ᣞ = ᣞ / ᣞ
          • ⁰ = ⁰ / ⁰

          Your minimum temperature regex doesn't try to match the character, but your maximum temp regex does.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          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