Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use regular expressions

Shulou Source: shulou.com Published: 2022-06-01 06:50:49 10月04日 Update

This article will explain in detail how to use regular expressions. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

The problem is, in a certain case, you have to give: http://jb51.net?a=1

The append parameter of this kind of url address is changed to: http://jb51.net?a=1&b=2

But how do you know that the same parameter name already exists, for example, in this case: http://jb51.net?a=1&a=2

There won't be any big mistakes in this, but the address bar looks bad. What are we going to do? Use the regular solution (I originally wanted to use php's string processing to solve the problem, but then I thought that if I wanted to learn the regular, I would use the regular to do it.)

The following is the background processing method I used to return to the previous page

Function _ goBack ($msg=null,$get=array ()) {

$url = $_ SESSION ['BACKURL']

If ($getgun array ())

Foreach ($get as $k = > $g) {

$url.='&'.$k.'='.$g;// appends all parameters to be appended first, regardless of whether they are repeated or not

If (count (explode ("& {$k} =", $url) > 2) {/ / if the current url string is split in the format "& a =", more than two split arrays are found, indicating that the string has duplicate parameters.

$url=preg_replace ("/ {$k} = [a-zA-Z0-9] * & /",'', $url); / / regular replacement all "& axix" is empty

}

}

Unset ($_ SESSION ['BACKURL'])

$this- > alert ($msg,$url)

}

A simple explanation:

"/ {$k} = [a-zA-Z0-9] * & /" if we assume $k = "a", that is, "/ a = [a-zA-Z0-9] * & /"

I don't know if I understand it correctly, but it actually means that the match starts with "a =", followed by any combination of numbers and letters ([a-zA-Z0-9] *: it means any single number or alphabetic character. It can be understood as a repetition of any previous character. For example, u * can be understood as: the u arrangement of uuuu., is unlimited, and the * here can be replaced by {0,}), followed by "&".

This is a very simple example, but looking at the description of regular grammar on the Internet, I always feel that I can't understand it. I don't know if it's because I understand the problem of ability or whether it's written too esoteric.

In addition, explain why this can remove repetition, the principle is very simple, http://jb51.net?a=1&a=2 is replaced by the "& axix" format character will not be deleted last, you know, we put the new parameter at the end of each time in our loop.

This is the end of the article on "how to use regular expressions". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Regular character parameter string article expression address letter situation meaning number more format problem processing interpretation good practical esoteric same Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Redmi Microsoft MySQL MariaDB