修饰符是php中正则表达式的一个重要的参考,下面我来给大家转一篇关于PHP中正则表达式模式修饰符详解,有需要了解的朋友可进入参考参考.
PHP模式修饰符又叫模式修正符,是在正则表达式的定界符之外使用.主要用来调整正则表达式的解释,提扩展了正则表达式在匹配、替换等操作的某些功能,增强了正则的能力.但是有很多地方的解释都是错误的,也容易误导别人,所以今天我把这个文档整理出来,供大家参考.
i 在和正则匹配是不区分大小写
m 将字符串视为多行.默认的正则开始“^”和结束“$”将目标字条串作为一单一的一“行”字符(甚至其中包括换行符也是如此).如果在修饰符中加上“m”,那么开始和结束将会指点字符串的每一行的开头就是“^”结束就是“$”.
s 如果设定了这个修正符,那么,被匹配的字符串将视为一行来看,包括换行符,换行符将被视为普通字符串.
x 忽略空白,除非进行转义的不被忽略.
e只用在preg_replace()函数中,在替换字符串中逆向引用做正常的替换,将其作为PHP代码求值,并用其结果来替换所搜索的字符串.
A 如果使用这个修饰符,那么表达式必须是匹配的字符串中的开头部分.比如说”/a/A”匹配”abcd”.
D 模式中的$字符权匹配目标字符的结尾.没有此选项时,如果最后一个字符是换行符的话,美元符号也会匹配此字符之前.如果设定了修正符m则忽略此项.
E 与”m”相反,如果使用这个修饰符,那么”$”将匹配绝对字符串的结尾,而不是换行符前面,默认就打开了这个模式.
U 贪婪模式,和问号的作用差不多,最大限度的匹配就是贪婪模式.
贪婪模式:
比如我们要匹配以字母“a”开头字母“b”结尾的字符串,但是需要匹配的字符串在“a”后面含有很多个“b”,比如“a bbbbbbbbbbbbbbbbb”,那正则表达式是会匹配第一个“b”还是最后一个“b”呢?如果你使用了贪婪模式,那么会匹配到最后一个“b”,反之只是匹配到第一个“b”.
PHP正则表达式贪婪模式使用实例:
1./a.+?b/
2./a.+b/U
对比不使用贪婪模式的实例如下:
1./a.+b/
上面使用了一个修饰符U,详见关于修饰符的介绍.
其他资料:
模式修正符: 解说正则表达式模式中使用的修正符
说明:下面列出了当前在 PCRE 中可能使用的修正符.括号中是这些修正符的内部 PCRE 名.修正符中的空格和换行被忽略,其它字符会导致错误.
i (PCRE_CASELESS)
如果设定此修正符,模式中的字符将同时匹配大小写字母.
m(PCRE_MULTILINE)
默认情况下,PCRE 将目标字符串作为单一的一“行”字符所组成的(甚至其中包含有换行符也是如此).“行起始”元字符(^)仅仅匹配字符串的起始,“行结束”元字符($)仅 仅匹配字符串的结束,或者最后一个字符是换行符时其前面(除非设定了 D 修正符).这和 Perl 是一样的.
当设定了此修正符,“行起始”和“行结束”除了匹配整个字符串开头和结束外,还分别匹配其中的换行符的之后和之前.这和 Perl 的 /m 修正符是等效的.如果目标字符串中没有“n”字符或者模式中没有 ^ 或 $,则设定此修正符没有任何效果.
s(PCRE_DOTALL)
如果设定了此修正符,模式中的圆点元字符(.)匹配所有的字符,包括换行符.没有此设定的话,则不包括换行符.这和 Perl 的 /s 修正符是等效的.排除字符类例如 [^a] 总是匹配换行符的,无论是否设定了此修正符.
x(PCRE_EXTENDED)
如果设定了此修正符,模式中的空白字符除了被转义的或在字符类中的以外完全被忽略,在未转义的字符类之外的 # 以及下一个换行符之间的所有字符,包括两头,也都被忽略.这和 Perl 的 /x 修正符是等效的,使得可以在复杂的模式中加入注释.然而注意,这仅适用于数据字符.空白字符可能永远不会出现于模式中的特殊字符序列,例如引入条件子模式 的序列 (?( 中间.
e
如果设定了此修正符,preg_replace() 在替换字符串中对逆向引用作正常的替换,将其作为 PHP 代码求值,并用其结果来替换所搜索的字符串.只有 preg_replace() 使用此修正符,其它 PCRE 函数将忽略之.
注: 本修正符在 PHP3 中不可用.
A(PCRE_ANCHORED)
如果设定了此修正符,模式被强制为“anchored”,即强制仅从目标字符串的开头开始匹配.此效果也可以通过适当的模式本身来实现(在 Perl 中实现的唯一方法).
D(PCRE_DOLLAR_ENDONLY)
如果设定了此修正符,模式中的美元元字符仅匹配目标字符串的结尾.没有此选项时,如果最后一个字符是换行符的话,美元符号也会匹配此字符之前(但不会匹配任何其它换行符之前).如果设定了 m 修正符则忽略此选项.Perl 中没有与其等价的修正符.
S
当一个模式将被使用若干次时,为加速匹配起见值得先对其进行分析.如果设定了此修正符则会进行额外的分析.目前,分析一个模式仅对没有单一固定起始字符的 non-anchored 模式有用.
U(PCRE_UNGREEDY)
本修正符反转了匹配数量的值使其不是默认的重复,而变成在后面跟上“?”才变得重复.这和 Perl 不兼容.也可以通过在模式之中设定 (?U) 修正符或者在数量符之后跟一个问号(如 .*?)来启用此选项.
X(PCRE_EXTRA)
此修正符启用了一个 PCRE 中与 Perl 不兼容的额外功能.模式中的任何反斜线后面跟上一个没有特殊意义的字母导致一个错误,从而保留此组合以备将来扩充.默认情况下,和 Perl 一样,一个反斜线后面跟一个没有特殊意义的字母被当成该字母本身.当前没有其它特性受此修正符控制.
u(PCRE_UTF8)
此修正符启用了一个 PCRE 中与 Perl 不兼容的额外功能.模式字符串被当成 UTF-8.本修正符在 Unix 下自 PHP 4.1.0 起可用,在 win32 下自 PHP 4.2.3 起可用.自 PHP 4.3.5 起开始检查模式的 UTF-8 合法性.
PHP正则表达式的基本语法:
一个正则表达式,分为三个部分:分隔符,表达式和修饰符.
分隔符可以是除了特殊字符以外的任何字符(比如"/ !"等等),常用的分隔符是"/".表达式由一些特殊字符(特殊字符详见下面)和非特殊的字符串组成,比如"[a-z0-9_-]+@[a-z0-9_-.]+"可以匹配一个简单的电子邮件字符串.修饰符是用来开启或者关闭某种功能/模式.下面就是一个完整的正则表达式的例子:
/hello.+?hello/is 上面的正则表达式"/"就是分隔符,两个"/"之间的就是表达式,第二个"/"后面的字符串"is"就是修饰符.
在表达式中如果含有分隔符,那么就需要使用转义符号"",比如"/hello.+?/hello/is".转义符号除了用于分隔符外还可以执行特殊字符,全部由字母构成的特殊字符都需要""来转义,比如"d"代表全体数字.
波比源码 » PHP中正则表达式模式修饰符详解
order levaquin levofloxacin 500mg price
order buspar online cheap cost cordarone 200mg buy ditropan online cheap
order prograf 1mg pill buy ropinirole generic cheap ursodiol 150mg
order chloroquine pill order olumiant 4mg for sale oral olumiant
buy lopressor 50mg sale cheap vardenafil buy levitra online cheap
clomid online order buy clomid 100mg casino slots
triamcinolone 10mg usa buy desloratadine pills clarinex 5mg price
order colchicine pill order colchicine generic best casinos
flagyl 200mg cost buy flagyl 200mg pill cheap sulfamethoxazole
buy viagra tablets viagra us order cialis 5mg online cheap
order cefuroxime pills robaxin 500mg oral methocarbamol 500mg oral
essays help cheap dissertation help ivermectin 3mg dose
sildenafil for sale estradiol online order lamotrigine pills
buy prednisone 5mg order prednisone pill amoxicillin 500mg over the counter
zithromax for sale prednisolone 5mg us cheap gabapentin 100mg
lasix oral order lasix pills order generic plaquenil 400mg
fildena 100mg cost cheap nolvadex 10mg order generic rhinocort
tadacip us buy voltaren 50mg buy indocin 75mg online cheap
albuterol 100mcg uk cipro 1000mg drug ciprofloxacin 1000mg sale
buy tiotropium bromide 9mcg for sale terazosin 1mg canada order terazosin 5mg without prescription
actos 30mg cheap cost pioglitazone 30mg sildenafil 100 mg
order tadalafil 40mg for sale medication for ed dysfunction buy cialis 5mg generic
casino play online poker real money casino games
ivermectin uk dapsone pill generic dapsone 100 mg
cost nifedipine 10mg buy perindopril 8mg generic order fexofenadine 120mg
play poker online free casino world legitimate canadian pharmacy list edit letter
buy ramipril 5mg glimepiride 4mg sale buy arcoxia 60mg
dissertation assistance buy sulfasalazine 500mg sulfasalazine 500mg without prescription
buy doxycycline generic cheap clindamycin order cleocin for sale
purchase temovate generic temovate pills buy cordarone 100mg
buy acetazolamide 250 mg online purchase imdur for sale azathioprine ca
order lanoxin 250 mg for sale buy telmisartan 20mg generic buy generic molnupiravir
buy coreg 25mg online coreg 25mg us purchase amitriptyline without prescription
order fosamax 35mg generic buy alendronate 35mg pills ibuprofen 400mg for sale
order priligy 60mg without prescription avana 200mg ca order domperidone pills
indomethacin 50mg for sale cenforce 100mg sale cenforce over the counter
doxycycline 100mg usa medrol 8mg without prescription order medrol pills
tricor 160mg uk order sildenafil order viagra 100mg pills
order cialis 10mg without prescription sildenafil mail order sildenafil 100mg
purchase minocycline minocin 100mg brand cheap hytrin 1mg
tadalafil women red ed pill where to buy ed pills
glycomet for sale online tamoxifen 20mg generic tamoxifen 10mg uk
buy modafinil 100mg generic promethazine order online order phenergan without prescription
deltasone 20mg us order amoxil 250mg sale amoxil 1000mg sale
order accutane 20mg pills isotretinoin 40mg without prescription order ampicillin 250mg
best ed drugs order pregabalin generic order proscar generic
buy ivermectin usa best non prescription ed pills order deltasone 40mg pill
isotretinoin 40mg cheap buy azithromycin 500mg without prescription azithromycin pill
purchase albuterol generic amoxiclav for sale online augmentin generic
cheap prednisolone 10mg order generic gabapentin 100mg furosemide 40mg sale
modafinil 200mg canada lisinopril brand metoprolol 100mg pills
dutasteride pills orlistat 60mg pills generic xenical
vibra-tabs price order zovirax acyclovir sale
imuran 25mg without prescription buy micardis 20mg for sale order naprosyn
oxybutynin 5mg over the counter buy prograf 5mg pills buy trileptal 300mg for sale
order simvastatin 10mg online cheap order sildalis sale sildalis cost
order dapsone 100mg without prescription asacol over the counter buy tenormin without a prescription
uroxatral sale order uroxatral for sale diltiazem 180mg drug
cost sildenafil 50mg real viagra 100mg brand name cialis