Regular Expression to match ranges of numbers except some certain ranges of numbers and some particular numbers [closed]

StackOverflow https://stackoverflow.com/questions/20450078

  •  30-08-2022
  •  | 
  •  

Question

I am creating a user security policy for each user. Each user security policy will have a regular expression statement in it.

Each webpage has its own unique ID in numeric format ranging from 0 to 100000. When a user accesses a webpage, the system will check the webpage's ID against the user's security policy expressing in regular expression.

For instance, a user has the access to all webpages (ID ranging from 1 - 100000) except webpages with ID number 2, 54, 109 to 2001 and 10521. How can I write an efficient regular expression to tackle this requirement?

Was it helpful?

Solution

You will find this will be both a programmatic nightmare to maintain and a significant performance overhead to accomplish the task. You would likely find it much more effective (and less cumbersome) to work with lists instead. You could store these lists in a database, or even using a cloud service provider that communicates with a RESTful and .

An example, written in php (mostly pseudo code), assuming the permissions have already been retrieved from the data storage:

//user1 is logged in and has access to the following array of allowed pages:
$loggedInUserPerms = array(1,6,99,821,983255);
if (in_array($pageID, $loggedInUserPerms))
{
    //the logged in user has access to this page
}
else
{
    //the logged in user doesn't, display access denied error
}

You could even expand this principle and use multidimensional arrays:

$loggedInUserPerms = array(
    1=>array("read"),
    6=>array("read","write"),
    9=>array("read","write"),
    821=>array("read","write","delete"),
    983255=>array("read")
);
if (in_array($pageID, $loggedInUserPerms))
{
    //the logged in user has access to this page

    //you can now handle the sub arrays as well 
    //to determine what level of access the user has.
}
else
{
    //the logged in user doesn't, display access denied error
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top