Learn regular expressions! Basic examples

Source: Internet
Author: User
Tags repetition

The problem is, in a certain situation to give: http://jb51.net? A = 1

This type of url address appending parameter changes 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

Although there will be no major errors, it looks bad in the address bar. What should we do? Solve it with regular expressions (I wanted to solve it with php Strings, but later I thought I would use regular expressions if I had to learn them)

The following is the background processing method for returning the previous page.
Copy codeThe Code is as follows:
Function _ goBack ($ msg = null, $ get = array ()){
$ Url = $ _ SESSION ['backurl'];
If ($ get! = Array ())
Foreach ($ get as $ k => $ g ){
$ Url. = '&'. $ k. '='. $ g; // append all parameters to be appended, whether repeated or not
If (count (explode ("& {$ k} =", $ url)> 2) {// If the url string is separated in the format of "& a =", more than two split arrays are found, indicating that the string has repeated parameters.
$ Url = preg_replace ("/{$ k} = [a-zA-Z0-9] * &/", '', $ url); // replace all" & a = x "with null
}
}
Unset ($ _ SESSION ['backurl']);
$ This-> alert ($ msg, $ url );
}

Simple explanation:
"/{$ K} = [a-zA-Z0-9] * &/" Suppose we assume $ k = "a", that is, "/a = [a-zA-Z0-9] * &/"

I do not know whether there is a wrong understanding, this means that the match is in fact to match with the beginning of "a =", followed by any number and letter combination ([a-zA-Z0-9] *: Meaning any single digit or letter character, * can be understood as the repetition of any character above, such as u * can be understood as: uuuu ....., there is no limit on the number of u sorting, here * can be replaced by {0,}), followed "&".

This is a simple example, but I can't understand the regular expression syntax on the Internet. I don't know whether it's my understanding or whether it's too esoteric.

Also explain, why can remove repetition, the principle is very simple, http://jb51.net? When a = 1 & a = 2 is replaced with the "& a = x" format character, the last one will not be deleted, you need to know that every time in our loop, we put the new parameters at the end.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.