In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
How to understand the regular expression of php, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
1. General atom:
\ d: number. \ d: except for numbers.
\ w: numbers, letters, underscores. \ W: except for numbers, letters, underscores.
\ s: blank character. \ s: except for the blank character.
2. Metacharacters:
. All characters except newline
*: match the previous content 0 times or more.
?: match the previous content 0 or 1 times.
+: appear one or more times.
^: it must start with it.
$: must end with it.
{n}: happens to occur n times.
{n,}: greater than or equal to n times
{nmagnetic m}: greater than or equal to n, less than or equal to m.
[]: is a collection that matches any character in parentheses, such as: [abc] is a match an or b or c.
(): the latter item is referenced or regarded as a whole.
[^]: reverse.
|: or
[-]: represents a range, such as [0-9], and the match is 0123456789.
3. Pattern modifier
I: case-insensitive.
M: splits the string with a delimiter to match each line in the string separately.
E: do some php syntax processing on the matching content.
S: fix "." Change the line of business.
U: cancel the greed mode.
X: ignore whitespace characters in the pattern.
A: it must start with this mode.
D: fixed the omission of "$" to "\ n".
U: used when doing utf-8 Chinese matching.
G: this expression can be matched globally.
Example:
Now you need to regularly validate an input box, and the non-integer I want to enter automatically becomes a null value.
The rules are as follows
# if you do not add / g, only the first match is returned, no matter how many times it is executed. If g is added, the first match is returned on the first execution, the second match is returned on the first execution, and so on. # / [^ 0-9] / g, this regular expression means to globally match non-numeric types and non-characters. / [^ 0-9 | -] / g
Once the rule is matched, leave the value of input empty.
Second, the latter item reference
If you want to remove only the b tag and keep only the string inside, you need to use the latter reference here. How about the latter reference, is to enclose. * and then write\ 1 when matching,\ 1 represents the matching content in the first parenthesis, of course, in order to prevent 1 from escaping, so we use\ 1.
Third, greedy mode
We want to match every b tag, that is, abc and bcd
/ / if the following rule is used to match, the abcbcd.$pattern='/.*/' will be matched.
So here we are going to use. *? To cancel greed, huh? It means to match the previous content 0 or 1 times.
/ / the regular expression is changed to $pattern='/.*?/';//. Of course, there is another way to cancel greed: add a U after it, that is, $pattern='/.*/U'.
But remember not. *? Use it with U.
4. Extension focus 1, common PCRE functions 2, Chinese matching examples:
$str = 'Chinese'
A. Use utf-8 to match Chinese:
$pattern='/ [\ x {4e00} -\ x {9fa5}] + / u'
B. Use GB2312 to match Chinese:
$pattern='/ ['.chr (0xb0).'-'.chr (0xf7).'] ['.chr (0xa1).'-'.chr (0xfe).'] /'; preg_match ($pattern,$str,$match)
3. Is it helpful for you to read the above after writing regular expressions? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.