search
php regular expressionApr 24, 2018 pm 02:37 PM
phpregularexpression

The content of this article is about regular expressions in PHP, which has certain reference value. Now I share it with you. Friends in need can refer to it

php regular expression

Regular expression is a grammatical rule that describes the result of a string. It is a specific formatting pattern that can match, replace, and intercept matching strings. Commonly used languages ​​basically have regular expressions, such as JavaScript, java, etc. In fact, as long as you understand the regular use of one language, it is relatively simple to use the regular rules of other languages. Okay, let’s start writing regular rules.

Related recommendations:
1. Regular expression syntax tutorial (including online testing tools)
2. PHP regular expression quick introduction video tutorial

When regular expressions match strings, they follow the following two basic principles:

1. The leftmost principle: Regular expressions always start from the target string. Starting from the leftmost position, matching is performed sequentially until the part that meets the requirements of the expression is matched, or until the end of the target string is matched.
2. The longest principle: For the matched target string, the regular expression will always match the longest part that meets the requirements of the regular expression; that is, the greedy mode

So what? To start, first start with the delimiter, which is commonly used to include /; #;~, which is used to indicate the beginning of a series of regular expressions. For example: ‘/a.*a/’. When the expression has too many escape characters, it is recommended to use # first, such as url;

$str = 'http://baidu.com';
$pattern = '/http:\/\/.*com/';//需要转义/
preg_match($pattern,$str,$match);
var_dump( $match);
$str = 'http://baidu.com';
$pattern = '#http://.*com#';//不需要转义/
preg_match($pattern,$str,$match);
var_dump( $match);

Now that you know how to write the beginning and the end, the next step is to judge the middle. Regular expressions are spliced ​​using atoms and metacharacters from left to right.

For example, 'zxcv', when matching, '/.*/', where .* represents zxcv.

So what are the common atoms and metacharacters?

\d Matches a numeric character. Equivalent to [0-9].
\D Matches a non-numeric character. Equivalent to [^0-9].
\f matches a form feed character. Equivalent to \x0c and \cL.
\n Matches a newline character. Equivalent to \x0a and \cJ.
\rmatches a carriage return character. Equivalent to \x0d and \cM.
\s Matches any whitespace character, including spaces, tabs, form feeds, etc. Equivalent to [ \f\n\r\t\v].
\S matches any non-whitespace character. Equivalent to [^ \f\n\r\t\v].
\tmatches a tab character. Equivalent to \x09 and \cI.
\v Matches a vertical tab character. Equivalent to \x0b and \cK.
\w Matches any word character including an underscore. Equivalent to '[A-Za-z0-9_]'.
\W matches any non-word character. Equivalent to ‘[^A-Za-z0-9_]’.
\xn Matches n, where n is the hexadecimal escape value. The hexadecimal escape value must be exactly two digits long. For example, '\x41' matches "A". ‘\x041’ is equivalent to ‘\x04’ & “1”. ASCII encoding can be used in regular expressions.
\nmIdentifies an octal escape value or a backreference. If \nm is preceded by at least nm get-subexpressions, nm is a backward reference. If \nm is preceded by at least n obtains, n is a backward reference followed by a literal m. If none of the previous conditions are met, and if n and m are both octal digits (0-7), \nm will match the octal escape value nm.
\nmlIf n is an octal number (0-3), and m and l are both octal numbers (0-7), then Matches the octal escape value nml.

\unUnicode characters represented by hexadecimal numbers. For example, \u00A9 matches the copyright symbol (?).

. Matches any single character except "\n"

^ Matches the beginning of the input string. In the character field [], it means negation, such as '[^\w]' equals '\w'; and ^\w means starting with a word character.

$ Matches the end position of the input string. For example '\w$' means ending with a word character.

? Matches the preceding subexpression zero or once is equivalent to {0,1}, for example, "do(es)?" can match "do" or "does".

* Matches the previous subexpression zero or more times , equivalent to {0,}. For example, zo* matches "z", "zo", 'zoo'.

Matches the previous subexpression one or more times, equivalent to {1,}. For example, 'zo ' can match "zo" and "zoo".

{n} n is a non-negative integer, matched n times. For example, 'o{2}' doesn't match "Bob" or 'Booob', but it does match the two o's in "food".

{n,} n is a non-negative integer. Match at least n times. For example, 'o{2,}' does not match the 'o' in "Bob", but it matches all o's in "foooood". 'o{1,}' is equivalent to 'o '. 'o{0,}' is equivalent to 'o*'.

{n,m} m and n are both non-negative integers, where n

[] Character set (character field). Matches any one of the characters contained. For example, '[abc]' matches 'a' in "plain".

() Match the content in () and get this match. With \n (n is an integer greater than 1), 'http://baidu.com' matches 'http://baidu' if the expression: '(\w) (:)\/\/.*\1' .comhttp',\1 means http.

(?:) matches but does not obtain the matching result and does not store it for later use. This is useful when using the "or" character (|) to combine parts of a pattern. For example, 'industr(?:y|ies) is a shorter expression than 'industry|industries'. If the above expression is changed to '(?:\w )(:)\/\/.*\1', then \1 is expressed as:

• |        x|y,匹配 x 或 y。例如,'z|food' 能匹配 "z" 或 "food"。'(z|f)ood' 则匹配 "zood" 或 "food"。

• [-]     字符范围。匹配指定范围内的任意字符。例如,'[a-z]' 可以匹配 'a' 到 'z' 范围内的任意小写字母字符。

• (?=pattern)正 向预查,在任何匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹       配不需要获取供以后使用。例如,'Windows (?=95|98|NT|2000)' 能匹配 "Windows 2000" 中的 "Windows" ,但不能匹配    "Windows 3.1" 中的 "Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹      配的搜索,而不是从包含预查的字符之后开始。
• (?!pattern)负 向预查,在任何不匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不     需要获取供以后使用。例如'Windows (?!95|98|NT|2000)' 能匹配 "Windows 3.1" 中的 "Windows",但不能匹配 "Windows    2000" 中的 "Windows"。预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开始下一次匹配的搜         索,而不是从包含预查的字符之后开始

有时候最后定界符会有一个字母,如‘/as.*/i’,那这个i又是什么呢,这就是模式修正符;

i表示在和模式进行匹配进不区分大小写
m将模式视为多行,使用^和$表示任何一行都可以以正则表达式开始或结束
s如果没有使用这个模式修正符号,元字符中的"."默认不能表示换行符号,将字符串视为单行
x表示模式中的空白忽略不计
e正则表达式必须使用在preg_replace替换字符串的函数中时才可以使用(讲这个函数时再说)
A以模式字符串开头,相当于元字符^
Z以模式字符串结尾,相当于元字符$

U正则表达式的特点:就是比较“贪婪”,使用该模式修正符可以取消贪婪模式

       例:

$str = 'asddadsdasd';
        $pattern = '/a.*d/';
        preg_match($pattern,$str,$match);
        var_dump($match) ;//asddadsdasd;
       $str = 'asddadsdasd';                                  
        $pattern = '/a.*d/U';//$pattern = '/a.*?d/';
        preg_match($pattern,$str,$match);
        var_dump($match) ;//asd

php常用正则函数;

    匹配:preg_match()与preg_match_all()

        1  preg_match($pattern,$subject,[array &$matches])
        2  preg_match_all($pattern,$subject,array &$matches)

      1只会匹配一次,2会把所有符合的字符串都匹配出来,并且放置到matches数组中,而且这两个函数都有一个整形的返回          值。1是一维数组,2是二维数组

替换:preg_replace()

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
搜索subject中匹配pattern的部分, 以replacement进行替换。

相关推荐:

PHP正则表达式分享

The above is the detailed content of php regular expression. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.