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. Negative regular expression

Negative regular expression

Scheduled Pinned Locked Moved Regular Expressions
regexquestiontutoriallearning
5 Posts 3 Posters 16 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.
  • L Offline
    L Offline
    Lukasz Nowakowski
    wrote on last edited by
    #1

    I read some articles about regular expressions, that much all strings except matched, but I can't get it working. In our system we have numeric user names. They are in form "99/999/999", where "9" is of course any number. We want to control access to some functionalities by using regular expressions. I mean we have a table with two columns, one contains regular expression, that matches some numbers, and second column contains a code, that informs, which functionality is available. So if we want users, that have numbers starting with "01" to access functionality "Functionality1", we have entry: 01/[0-9]{3}/[0-9]{3}, Functionality1. But there's a functionality, that is inaccessible by a group of users, that have a number, that starts with 23/123/. And I can't figure out how to do it (in a simple form). The only thing I can make working is to combine ranges. So I have something like: ([0-1][0-9]/[0-9]{3}/[0-9]{3})|(2[0-2]/[0-9]{3}/[0-9]{3})... And this way I can combine a number of ranges to match only those, I want to match. But this makes it quite difficult to check for errors and modify. How can I make a regex, that excludes all numbers, that start with 23/123 without specifying all ranges, that are allowed?

    Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

    P D 2 Replies Last reply
    0
    • L Lukasz Nowakowski

      I read some articles about regular expressions, that much all strings except matched, but I can't get it working. In our system we have numeric user names. They are in form "99/999/999", where "9" is of course any number. We want to control access to some functionalities by using regular expressions. I mean we have a table with two columns, one contains regular expression, that matches some numbers, and second column contains a code, that informs, which functionality is available. So if we want users, that have numbers starting with "01" to access functionality "Functionality1", we have entry: 01/[0-9]{3}/[0-9]{3}, Functionality1. But there's a functionality, that is inaccessible by a group of users, that have a number, that starts with 23/123/. And I can't figure out how to do it (in a simple form). The only thing I can make working is to combine ranges. So I have something like: ([0-1][0-9]/[0-9]{3}/[0-9]{3})|(2[0-2]/[0-9]{3}/[0-9]{3})... And this way I can combine a number of ranges to match only those, I want to match. But this makes it quite difficult to check for errors and modify. How can I make a regex, that excludes all numbers, that start with 23/123 without specifying all ranges, that are allowed?

      Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

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

      The exact details depends on which regex engine you are using, but in most, the construct [^2-5] for example would match any character NOT in the range 2 to 5 inclusive. Give it a try with your favourite regex tester. (There are some good ones online.) Cheers, Peter

      Software rusts. Simon Stephenson, ca 1994.

      L 1 Reply Last reply
      0
      • P Peter_in_2780

        The exact details depends on which regex engine you are using, but in most, the construct [^2-5] for example would match any character NOT in the range 2 to 5 inclusive. Give it a try with your favourite regex tester. (There are some good ones online.) Cheers, Peter

        Software rusts. Simon Stephenson, ca 1994.

        L Offline
        L Offline
        Lukasz Nowakowski
        wrote on last edited by
        #3

        I'm using .NET built-in engine. I tried, but it ends up with very complicated expression. I think we'll try to find another solution.

        Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

        1 Reply Last reply
        0
        • L Lukasz Nowakowski

          I read some articles about regular expressions, that much all strings except matched, but I can't get it working. In our system we have numeric user names. They are in form "99/999/999", where "9" is of course any number. We want to control access to some functionalities by using regular expressions. I mean we have a table with two columns, one contains regular expression, that matches some numbers, and second column contains a code, that informs, which functionality is available. So if we want users, that have numbers starting with "01" to access functionality "Functionality1", we have entry: 01/[0-9]{3}/[0-9]{3}, Functionality1. But there's a functionality, that is inaccessible by a group of users, that have a number, that starts with 23/123/. And I can't figure out how to do it (in a simple form). The only thing I can make working is to combine ranges. So I have something like: ([0-1][0-9]/[0-9]{3}/[0-9]{3})|(2[0-2]/[0-9]{3}/[0-9]{3})... And this way I can combine a number of ranges to match only those, I want to match. But this makes it quite difficult to check for errors and modify. How can I make a regex, that excludes all numbers, that start with 23/123 without specifying all ranges, that are allowed?

          Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

          D Offline
          D Offline
          dasblinkenlight
          wrote on last edited by
          #4

          I know this is probably not the answer you are looking for, but you can add a third column to your table of type boolean. Then you can make decisions to allow or deny access to functionality-X based on the content of your regexp and the flag: if the flag is set, the expression must match in order for the functionality to be available; if the flag is not set, the expression must not match in order for the functionality to be available. In other words, functionality is available only when regexp.IsMatch is equal to the flag from the table. This will improve your maintainability as well, because the target regexps will be a lot more readable.

          L 1 Reply Last reply
          0
          • D dasblinkenlight

            I know this is probably not the answer you are looking for, but you can add a third column to your table of type boolean. Then you can make decisions to allow or deny access to functionality-X based on the content of your regexp and the flag: if the flag is set, the expression must match in order for the functionality to be available; if the flag is not set, the expression must not match in order for the functionality to be available. In other words, functionality is available only when regexp.IsMatch is equal to the flag from the table. This will improve your maintainability as well, because the target regexps will be a lot more readable.

            L Offline
            L Offline
            Lukasz Nowakowski
            wrote on last edited by
            #5

            If we didn't redesign our solution, this would probably be our course of action. So (although I won't use your solution ;-) ), I'm giving you a "5".

            Don't forget to rate answer, that helped you. It will allow other people find their answers faster.

            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