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. C#
  4. Regex.Matches() problem

Regex.Matches() problem

Scheduled Pinned Locked Moved C#
regexhelpquestion
5 Posts 3 Posters 0 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.
  • S Offline
    S Offline
    Seishin
    wrote on last edited by
    #1

    Hi! I've got a string: string str = ">200 <=250"; and a regular expression: Regex rx = new Regex ("^(>=?|<=?|=) *[0-9]+ *$"); the way I see it (understand regex) with rx.Matches(str); I should get two matches (">200" and "<=250") but I get an empty collection.. did I do something wrong? thanks for any help!! Seishin

    life is study!!!

    L S 2 Replies Last reply
    0
    • S Seishin

      Hi! I've got a string: string str = ">200 <=250"; and a regular expression: Regex rx = new Regex ("^(>=?|<=?|=) *[0-9]+ *$"); the way I see it (understand regex) with rx.Matches(str); I should get two matches (">200" and "<=250") but I get an empty collection.. did I do something wrong? thanks for any help!! Seishin

      life is study!!!

      L Offline
      L Offline
      LuCasn
      wrote on last edited by
      #2

      You probably want this: Regex rx = new Regex ("(>=|<=|=)\d+"); x.Matches(str); This matches all numbers with a ">", ">=", "<=" before it, no matter where in the text it is. It also matches in-text, it would for example also match "thisisatext>=500", which you could exclude by adding "\b"'s for word-end-checking, like this: Regex rx = new Regex ("\b(>=|<=|=)\d+\b");

      S 1 Reply Last reply
      0
      • S Seishin

        Hi! I've got a string: string str = ">200 <=250"; and a regular expression: Regex rx = new Regex ("^(>=?|<=?|=) *[0-9]+ *$"); the way I see it (understand regex) with rx.Matches(str); I should get two matches (">200" and "<=250") but I get an empty collection.. did I do something wrong? thanks for any help!! Seishin

        life is study!!!

        S Offline
        S Offline
        Skippums
        wrote on last edited by
        #3

        Two things: 1. You are REQUIRING your match to occur for the entire string since you are using '^' and '$' at the beginning and end, respectively, and 2. You may need to escape the '>' and '<' characters. Implementing both of the above, your regex becomes...

        Regex rx = new Regex(@"(\>=?|\<=?|=)\s*\d+\s*");

        Let me know if this works as desired,

        Sounds like somebody's got a case of the Mondays -Jeff

        S 1 Reply Last reply
        0
        • L LuCasn

          You probably want this: Regex rx = new Regex ("(>=|<=|=)\d+"); x.Matches(str); This matches all numbers with a ">", ">=", "<=" before it, no matter where in the text it is. It also matches in-text, it would for example also match "thisisatext>=500", which you could exclude by adding "\b"'s for word-end-checking, like this: Regex rx = new Regex ("\b(>=|<=|=)\d+\b");

          S Offline
          S Offline
          Seishin
          wrote on last edited by
          #4

          thanks.. "(>=?|<=?|=) *\d+" works fine.. i wanted to match '>' '<' as well so i had to add '?' after '='.

          life is study!!!

          1 Reply Last reply
          0
          • S Skippums

            Two things: 1. You are REQUIRING your match to occur for the entire string since you are using '^' and '$' at the beginning and end, respectively, and 2. You may need to escape the '>' and '<' characters. Implementing both of the above, your regex becomes...

            Regex rx = new Regex(@"(\>=?|\<=?|=)\s*\d+\s*");

            Let me know if this works as desired,

            Sounds like somebody's got a case of the Mondays -Jeff

            S Offline
            S Offline
            Seishin
            wrote on last edited by
            #5

            yaeh. the problem was with ^ and $.. removing them solved the issue.. thanks!

            life is study!!!

            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