We use regular expressions to master various functions and structures. solving practical problems is the real purpose. To solve the real problem, we must have a solution to the problem. The function of regular expressions can be divided into three types of logic. For the convenience of expression, we are called sum, or, not.
Recently, when using CI for the personal salary management system, you need to verify whether the user is logged on and using specific functions, and use the regular expression-Non. The requirements are as follows:
The path/user,/user/login,/user/register does not need to be intercepted. In fact, all paths such as/profile,/company, And/work must be intercepted, then, check whether user_id exists in the session. If no, jump to/user/login.
Preliminary Observation:
You only need to match the/user. The initial code is as follows:
01
<? Php
02
Class Acl {
03
04
Private $ CI;
05
06
Public function _ construct (){
07
$ This-> CI = & get_instance ();
08
}
09
10
Public function auth (){
11
If (! Preg_match ('/^ user. * $/', uri_string ())){
12
$ User_id = $ this-> CI-> session-> userdata ('user _ id ');
13
If (empty ($ user_id )){
14
Redirect ('/user ');
15
Return;
16
}
17
}
18
}
19
}
The user/change_password is detected during the test. This function requires the customer to log on first. * Excluding the user/change_password, that is, the "Non" in the regular expression is used ".
"Non" is the most difficult logical relationship to process in a regular expression. Because there is no directly corresponding structure, the "Non" processing is more difficult.
The simplest "Non" means that a character cannot appear here, which is usually intuitive and seems to be used as an excluded character group [^…] You can solve this problem. For example, double quotation marks match strings, and the first and last two double quotation marks are very easy to match. The content is certainly not double quotation marks (escape is not considered for the moment), so you can use [^ "] to represent it, its length is not determined, so it is limited by *. Therefore, the entire expression is "[^"] * ", which is very simple.
But is everything so simple? Let's take cat and cut as an example. If you want to match the word starting with "t" but not ending with "cut", you can write it as "c [^ u] t". Is it OK?
This expression indicates that the first letter is c, followed by a character not u, followed by t. Yes, it does not match cut or cat. However, it cannot match charts, conducting CT, and court, because [^ u] means matching a character that is not u.
Then, change [^ u] to [^ u] + to solve the problem. But is that true? [^ U] + indicates one or more (to infinity) characters, but each character cannot be u. Therefore, although c [^ u] + t can match cat and chart, it cannot match the conducting and court.
In fact, you can use the sequential negative view function to solve the cut and Path Matching Problem.
(?! Cut) is used for this judgment. It determines whether the string can be matched by the cut, and does not move the "current position ". So we put it at the beginning of the expression to get (?! Cut) c [a-z] + t. The logic of this expression is: use c [a-z] + t to the right only when the string at the right of the current position cannot be matched by cut.
If we need to exclude cat and cut, we can change the negative order (?! C [au] t ). In this way, we can ensure that the matching is definitely not cat or cut. Www.2cto.com
Back to the top question. The Code is as follows:
01
<? Php
02
Class Acl {
03
04
Private $ CI;
05
06
Public function _ construct (){
07
$ This-> CI = & get_instance ();
08
}
09
10
Public function auth (){
11
If (! Preg_match ('/^ (?! User \/change_password) user. * $/', uri_string ())){
12
$ User_id = $ this-> CI-> session-> userdata ('user _ id ');
13
If (empty ($ user_id )){
14
Redirect ('/user ');
15
Return;
16
}
17
}
18
}
19
}
Author: kxt