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