Home Backend Development PHP Tutorial Regular Expressions-Non_PHP Tutorial

Regular Expressions-Non_PHP Tutorial

Jul 13, 2016 pm 05:52 PM
use actual us master regular skilled of structure expression solve question No

We use regular expressions, and mastering various functions and structures is just a means, and solving actual problems is the real purpose. To solve the real problem, you must have an idea to solve the problem. In the final analysis, the functions of regular expressions can be summarized into three kinds of logic. For the convenience of expression, we call them AND, OR and NOT respectively.

When I was using CI recently to build a personal salary management system, I needed to verify whether the user was logged in and using specific functions, so I used the regular expression - non. The requirements are as follows:

The paths /user, /user/login, /user/register do not need to be intercepted. In fact, paths such as /profile, /company, /work must be intercepted, and then check whether user_id exists in the session. If not, jump Go to /user/login

Initial observations:

Just match /user. The preliminary code is as follows:

01
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
}
During the test, I discovered that I missed detecting user/change_password. This function requires the customer to log in first. Here, user/change_password must be excluded in user.*, which means that the "non" in the regular expression must be used as mentioned earlier.

"Not" is the most difficult logical relationship to handle in regular expressions. Because there is no direct corresponding structure, the processing of "not" is more difficult.

The simplest "non" means that a certain character cannot appear here. This is usually very intuitive. It seems that it can be solved by using the exclusive character group [^...]. For example, when matching double quote strings, the first and last double quotes are easy to match. The content is definitely not double quotes (escaping is not considered for the time being), so it can be represented by [^"], and its length is uncertain. So it is qualified with *, so the whole expression is "[^"]*", which is very simple.

But are things really that simple? Let’s take the example of cat and cut. If you want to match words starting with c and ending with t, but don’t want to match cut, you can write c[^u]t. Is that okay?

This expression means: the first letter is c, followed by a character that is not u, followed by t. Yes, it does not match cut, it can also match cat. However, it cannot match chart, conduct, court, etc., because [^u] means: match a character that is not u.

Then, change [^u] to [^u]+, this should solve the problem. But is it really so? [^u]+ means one or several (up to infinity) characters, but each character cannot be u. Therefore, although c[^u]+t can match cat and chart, it cannot match conduct and court.

In fact, the path matching problem between cut and me can be solved by using the sequential negative look-around function.

(?!cut) is used for this kind of judgment. It judges whether the subsequent string can be matched by cut and will not move the "current position". So we put it at the very beginning of the expression and get (?!cut)c[a-z]+t. The logic of this expression is: only if the string to the right of the current position cannot be matched by cut, start from here and try c[a-z]+t to the right.

If we go one step further and need to exclude cat and cut, we can change the negative order look around to (?!c[au]t). This will ensure that the matched one is definitely not cat or cut. www.2cto.com

Back to the top question. Just go to the code, the code is as follows:

01
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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478096.htmlTechArticleWe use regular expressions and mastering various functions and structures is just a means, solving actual problems is the real thing Purpose. To solve real problems, you must have problem-solving ideas...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Five tips to teach you how to solve the problem of Black Shark phone not turning on! Five tips to teach you how to solve the problem of Black Shark phone not turning on! Mar 24, 2024 pm 12:27 PM

As smartphone technology continues to develop, mobile phones play an increasingly important role in our daily lives. As a flagship phone focusing on gaming performance, the Black Shark phone is highly favored by players. However, sometimes we also face the situation that the Black Shark phone cannot be turned on. At this time, we need to take some measures to solve this problem. Next, let us share five tips to teach you how to solve the problem of Black Shark phone not turning on: Step 1: Check the battery power. First, make sure your Black Shark phone has enough power. It may be because the phone battery is exhausted

The driver cannot be loaded on this device. How to solve it? (Personally tested and valid) The driver cannot be loaded on this device. How to solve it? (Personally tested and valid) Mar 14, 2024 pm 09:00 PM

Everyone knows that if the computer cannot load the driver, the device may not work properly or interact with the computer correctly. So how do we solve the problem when a prompt box pops up on the computer that the driver cannot be loaded on this device? The editor below will teach you two ways to easily solve the problem. Unable to load the driver on this device Solution 1. Search for "Kernel Isolation" in the Start menu. 2. Turn off Memory Integrity, and it will prompt "Memory Integrity has been turned off. Your device may be vulnerable." Click behind to ignore it, and it will not affect the use. 3. The problem can be solved after restarting the machine.

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? Mar 22, 2024 am 08:06 AM

With the continuous development of social media, Xiaohongshu has become a platform for more and more young people to share their lives and discover beautiful things. Many users are troubled by auto-save issues when posting images. So, how to solve this problem? 1. How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? 1. Clear the cache First, we can try to clear the cache data of Xiaohongshu. The steps are as follows: (1) Open Xiaohongshu and click the "My" button in the lower right corner; (2) On the personal center page, find "Settings" and click it; (3) Scroll down and find the "Clear Cache" option. Click OK. After clearing the cache, re-enter Xiaohongshu and try to post pictures to see if the automatic saving problem is solved. 2. Update the Xiaohongshu version to ensure that your Xiaohongshu

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

See all articles