通用模式,定界符,通常使用 "/"做为定界符开始和结束,也可以使用"#"。
什么时候使用"#"呢?一般是在你的字符串中有很多"/"字符的时候,因为正则的时候这种字符需要转义,比如uri。
使用"/"定界符的代码如下.
- ?$regex = '/^http://([w.]+)/([w]+)/([w]+).html$/i';
- $str = 'http://www.phpfensi.com/show_page/id_ABCDEFG.html';
- $matches = array();
- if(preg_match($regex, $str, $matches)){
- var_dump($matches);
- }
- echo "n";
preg_match中的$matches[0]将包含与整个模式匹配的字符串,使用"#"定界符的代码如下.这个时候对"/"就不转义!
- ?$regex = '#^http://([w.]+)/([w]+)/([w]+).html$#i';
- $str = 'http://www.phpfensi.com/show_page/id_ABCDEFG.html';
- $matches = array();
- if(preg_match($regex, $str, $matches)){
- var_dump($matches);
- }
- echo "n";
修饰符:用于改变正则表达式的行为,我们看到的('/^http://([w.]+)/([w]+)/([w]+).html/i')中的最后一个"i"就是修饰符,表示忽略大小写,还有一个我们经常用到的是"x"表示忽略空格,贡献代码:
- ?$regex = '/HELLO/';
- $str = 'hello word';
- $matches = array();
- if(preg_match($regex, $str, $matches)){
- echo 'No i:Valid Successful!',"n";
- }
- if(preg_match($regex.'i', $str, $matches)){
- echo 'YES i:Valid Successful!',"n";
- }
字符域:[w]用方括号扩起来的部分就是字符域,限定符:如[w]{3,5}或者[w]*或者[w]+这些[w]后面的符号都表示限定符,现介绍具体意义。
{3,5}表示3到5个字符。{3,}超过3个字符,{,5}最多5个,{3}三个字符,* 表示0到多个,+ 表示1到多个。
脱字符号^:> 放在字符域(如:[^w])中表示否定(不包括的意思)——“反向选择”
> 放在表达式之前,表示以当前这个字符开始。(/^n/i,表示以n开头),注意,我们经常管""叫"跳脱字符"。用于转义一些特殊符号,如".","/"
界符,正则表达式的形式一般如下:/love/
其中位于“/”定界符之间的部分就是将要在目标对象中进行匹配的模式。
元字符:就是指那些在正则表达式中具有特殊意义的专用字符,可以用来规定其前导字符(即位于元字符前面的字符)在目标对象中的出现模式。
较为常用的元字符包括: “+”, “*”,以及 “?”。
“+”元字符规定其前导字符必须在目标对象中连续出现一次或多次
“*”元字符规定其前导字符必须在目标对象中出现零次或连续多次,
“?”元字符规定其前导字符必须在目标对象中连续出现零次或一次。
下面,就让我们来看一下正则表达式元字符的具体应用。
/fo+/,因为上述正则表达式中包含“+”元字符(它前面的“o”是前导字符),表示可以与目标对象中的“fool”, “fo”等在字母f后面连续出现一个或多个字母o的字符串相匹配。
除了元字符之外,用户还可以精确指定模式在匹配对象中出现的频率。例如,
/jim{2,6}/,上述正则表达式规定字符m可以在匹配对象中连续出现2-6次,因此,上述正则表达式可以同jimmy或jimmmmmy等字符串相匹配。
其它几个重要的元字符的使用方式。
s:用于匹配单个空格符,包括tab键和换行符;
S:用于匹配除单个空格符之外的所有字符;
d:用于匹配从0到9的数字;
w:用于匹配字母,数字或下划线字符;
W:用于匹配所有与w不匹配的字符;
. :用于匹配除换行符之外的所有字符。
(说明:我们可以把s和S以及w和W看作互为逆运算)
下面,我们就通过实例看一下如何在正则表达式中使用上述元字符。
/s+/:上述正则表达式可以用于匹配目标对象中的一个或多个空格字符。
除了我们以上所介绍的元字符之外,正则表达式中还具有另外一种较为独特的专用字符,即定位符。
定位符:用于规定匹配模式在目标对象中的出现位置。
较为常用的定位符包括: “^”, “$”, “b” 以及 “B”。
“^”定位符规定匹配模式必须出现在目标字符串的开头
“$”定位符规定匹配模式必须出现在目标对象的结尾
b定位符规定匹配模式必须出现在目标字符串的开头或结尾的两个边界之一
“B”定位符则规定匹配对象必须位于目标字符串的开头和结尾两个边界之内,即匹配对象既不能作为目标字符串的开头,也不能作为目标字符串的结尾。同样,我们
也可以把“^”和“$”以及“b”和“B”看作是互为逆运算的两组定位符。举例来说:/^hell/,因为上述正则表达式中包含“^”定位符,所以可以与目标对象中以 “hell”, “hello”或 “hellhound”开头的字符串相匹配。
/ar$/:因为上述正则表达式中包含“$”定位符,所以可以与目标对象中以 “car”, “bar”或 “ar” 结尾的字符串相匹配。
/bbom/:因为上述正则表达式模式以“b”定位符开头,所以可以与目标对象中以 “bomb”, 或 “bom”开头的字符串相匹配。
/manb/:因为上述正则表达式模式以“b”定位符结尾,所以可以与目标对象中以 “human”, “woman”或 “man”结尾的字符串相匹配。
为了能够方便用户更加灵活的设定匹配模式,正则表达式允许使用者在匹配模式中指定某一个范围而不局限于具体的字符。例如:
/[A-Z]/:上述正则表达式将会与从A到Z范围内任何一个大写字母相匹配。
/[a-z]/:上述正则表达式将会与从a到z范围内任何一个小写字母相匹配。
/[0-9]/:上述正则表达式将会与从0到9范围内任何一个数字相匹配。
/([a-z][A-Z][0-9])+/:上述正则表达式将会与任何由字母和数字组成的字符串,如 “aB0” 等相匹配。这里需要提醒用户注意的一点就是可以在正则表达式中使用 “()” 把字符串组合在一起。
“()”符号:包含的内容必须同时出现在目标对象中。因此,上述正则表达式将无法与诸如 “abc”等的字符串匹配,因为“abc”中的最后一个字符为字母而非数字。
如果我们希望在正则表达式中实现类似编程逻辑中的“或”运算,在多个不同的模式中任选一个进行匹配的话,可以使用管道符: “|”。例如:/to|too|2/
上述正则表达式将会与目标对象中的 “to”, “too”, 或 “2” 相匹配。
否定符:“[^]”。与我们前文所介绍的定位符 “^” 不同,否定符 “[^]”规定目标对象中不能存在模式中所规定的字符串。例如:/[^A-C]/
上述字符串将会与目标对象中除A,B,和C之外的任何字符相匹配。一般来说,当“^”出现在 “[]”内时就被视做否定运算符;而当“^”位于“[]”之外,或没有“[]”时,则应当被视做定位符。
最后,当用户需要在正则表达式的模式中加入元字符,并查找其匹配对象时,可以使用
转义符:“”。例如:/Th*/
上述正则表达式将会与目标对象中的“Th*”而非“The”等相匹配。
实际经验介绍:还是得说说 ^ 和 $ 他们是分别用来匹配字符串的开始和结束,以下分别举例说明:
“^The”:开头一定要有”The”字符串;
“of despair$”:结尾一定要有”of despair” 的字符串;那么,“^abc$”:就是要求以abc开头和以abc结尾的字符串,实际上是只有abc匹配;
“notice”:匹配包含notice的字符串;你可以看见如果你没有用我们提到的两个字符(最后一个例子),就是说模式(正则表达式)可以出现在被检验字符串的任何地方,你没有把他锁定到两边。
接着,说说 ‘*’ ‘+’ 和 ‘?’,他们用来表示一个字符可以出现的次数或者顺序,他们分别表示:
“zero or more”相当于{0,}
“one or more”相当于{1,}
“zero or one.”相当于{0,1}
这里是一些例子:“ab*”:和ab{0,}同义,匹配以a开头,后面可以接0个或者N个b组成的字符串(”a”, “ab”, “abbb”, 等);
“ab+”:和ab{1,}同义,同上条一样,但最少要有一个b存在 (”ab” “abbb”等);
“ab?”:和ab{0,1}同义,可以没有或者只有一个b;
“a?b+$”:匹配以一个或者0个a再加上一个以上的b结尾的字符串。
要点:’*’ ‘+’ 和 ‘?’ 只管它前面那个字符。
你也可以在大括号里面限制字符出现的个数,比如:
“ab{2}”: 要求a后面一定要跟两个b(一个也不能少)(”abb”);
“ab{2,}”: 要求a后面一定要有两个或者两个以上b(如”abb” “abbbb” 等);
“ab{3,5}”: 要求a后面可以有2-5个b(”abbb”, “abbbb”, or “abbbbb”)。现在我们把一定几个字符放到小括号里,比如:
“a(bc)*”: 匹配 a 后面跟0个或者一个”bc”;
“a(bc){1,5}”: 一个到5个 “bc”;
还有一个字符 ‘|’,相当于OR操作:
“hi|hello”: 匹配含有”hi” 或者 “hello” 的 字符串;
“(b|cd)ef”: 匹配含有 “bef” 或者 “cdef”的字符串;
“(a|b)*c”: 匹配含有这样多个(包括0个)a或b,后面跟一个c的字符串;
一个点(’.’)可以代表所有的单一字符,不包括” ”
如果,要匹配包括” ”在内的所有单个字符,怎么办?用’[ .]’这种模式。
“a.[0-9]”: 一个a加一个字符再加一个0到9的数字;
“^.{3}$”: 三个任意字符结尾。
中括号括住的内容只匹配一个单一的字符
“[ab]”: 匹配单个的 a 或者 b ( 和 “a│b” 一样);
“[a-d]”: 匹配’a’ 到’d’的单个字符 (和”a│b│c│d” 还有 “[abcd]”效果一样);
一般我们都用[a-zA-Z]来指定字符为一个大小写英文:
“^[a-zA-Z]”: 匹配以大小写字母开头的字符串;
“[0-9]%”: 匹配含有 形如 x% 的字符串;
“,[a-zA-Z0-9]$”: 匹配以逗号再加一个数字或字母结尾的字符串;
你也可以把你不想要得字符列在中括号里,你只需要在总括号里面使用’^’ 作为开头 “%[^a-zA-Z]%” 匹配含有两个百分号里面有一个非字母的字符串。
要点:^用在中括号开头的时候,就表示排除括号里的字符。
为了PHP能够解释,你必须在这些字符面前后加”,并且将一些字符转义。
不要忘记在中括号里面的字符是这条规路的例外—在中括号里面,所有的特殊字符,包括(”),都将失去他们的特殊性质 “[*+?{}.]”匹配含有这些字符的字符串:
还有,正如regx的手册告诉我们:”如果列表里含有’]’,最好把它作为列表里的第一个字符(可能跟在’^’后面)。如果含有’-’,最好把它放在最前面或者最后面, or 或者一个范围的第二个结束点[a-d-0-9]中间的‘-’将有效。
看了上面的例子,你对{n,m}应该理解了吧。要注意的是,n和m都不能为负整数,而且n总是小于m。这样,才能 最少匹配n次且最多匹配m次。如”p{1,5}”将匹配“pvpppppp”中的前五个p
下面说说以开头的
b 书上说他是用来匹配一个单词边界,就是…比如’veb’,可以匹配love里的ve而不匹配very里有ve
B 正好和上面的b相反。
正则表达式的其他用法
提取字符串:ereg() and eregi() 有一个特性是允许用户通过正则表达式去提取字符串的一部分(具体用法你可以阅读手册)。比如说,我们想从 path/URL 提取文件名,下面的代码就是你需要:
ereg(”([^/]*)$”, $pathOrUrl, $regs);echo $regs[1];
高级的代换:ereg_replace() 和 eregi_replace()也是非常有用的,假如我们想把所有的间隔负号都替换成逗号:
ereg_replace(”[ t]+”, “,”, trim($str));
以下为引用的内容:
- preg_match()和preg_match_all()
- preg_quote()
- preg_split()
- preg_grep()
- preg_replace()
函数的具体使用,我们可以通过PHP手册来找到,下面分享一些平时积累的正则表达式,匹配action属性,以下为引用的内容:
- $str = '';
- $match = '';
- preg_match_all('/s+action="(?!http:)(.*?)"s/', $str, $match);
- print_r($match);
在正则中使用回调函数,以下为引用的内容:
- /**
- * replace some string by callback function
- *
- */
- function callback_replace() {
- $url = 'http://esfang.house.sina.com.cn';
- $str = '';
- $str = preg_replace ( '/(?<=saction=")(?!http:)(.*?)(?="s)/e', 'search($url, 1)', $str );
- echo $str;
- }
- function search($url, $match){
- return $url . '/' . $match;
- }
带断言的正则匹配,以下为引用的内容:
- $match = '';
- $str = 'xxxxxx.com.cn bold font
- paragraph text
- ';
- preg_match_all ( '/(?<=<(w{1})>).*(?=)/', $str, $match );
- echo "匹配没有属性的HTML标签中的内容:";
- print_r ( $match );
替换HTML源码中的地址,以下为引用的内容:
- $form_html = preg_replace ( '/(?<=saction="|ssrc="|shref=")(?!http:|javascript)(.*?)(?="s)/e', 'add_url($url, '1')', $form_html );
元字符:在上面的例子中,^ 、d 及 $ 等这些符号,代表了特定的匹配意义,我们称之为元字符,常用的元字符如下:
元字符 说明
. 匹配除换行符意外的任意字符
w 匹配字母或数字或下划线
s 匹配任意的空白符
d 匹配数字
b 匹配单词的开始或结束
^ 匹配字符串的开始
$ 匹配字符串的结束
[x] 匹配x字符,如匹配字符串中的 a、b 和 c 字符
W w的反义,即匹配任意非字母,数字,下划线和汉字的字符
S s的反义,即匹配任意非空白符的字符
D d的反义,即匹配任意非数字的字符
B b的反义,即不是单词开头或结束的位置
[^x] 匹配除了 x 意外的任意字符,如 [^abc] 匹配除了 abc 这几个字母之外的任意字符
波比源码 » 常用的php正则表达式收集
levaquin 250mg us buy levofloxacin 500mg sale
order levofloxacin 250mg sale buy levaquin 500mg online cheap
order dutasteride pill buy flomax 0.4mg for sale order ondansetron 4mg pill
generic avodart 0.5mg buy avodart 0.5mg pills ondansetron brand
order generic aldactone 100mg order finasteride 1mg without prescription fluconazole online
order ampicillin generic buy erythromycin 500mg sale erythromycin pills
generic ampicillin cheap cephalexin 500mg buy erythromycin online cheap
buy generic sildenafil 100mg buy tamoxifen for sale methocarbamol drug
purchase fildena pill order robaxin order methocarbamol without prescription
suhagra 50mg without prescription oral estradiol 1mg estradiol pill
cost lamictal 200mg minipress 1mg drug retin generic
order lamictal 200mg pill buy retin cream online cheap tretinoin online order
tadalis 20mg for sale order tadalafil pill voltaren online buy
indomethacin 50mg uk purchase amoxicillin generic buy trimox sale
order cialis 20mg for sale tadalafil for sale sildenafil overnight shipping
indomethacin 50mg uk terbinafine ca amoxicillin medication
generic arimidex 1mg order clarithromycin 250mg generic buy generic viagra 50mg
rx pharmacy online cialis ed medication sildenafil 50mg price
order deltasone for sale sildenafil 100mg oral viagra 100mg pills for men
order prednisone 10mg sale sildenafil otc viagra 100mg ca
order doxycycline 100mg pill furosemide 40mg pill furosemide us
altace 10mg price irbesartan drug order azelastine 10ml
buspar drug amiodarone 100mg usa buy oxybutynin 5mg sale
terazosin 5mg drug order actos 30mg sale buy azulfidine 500 mg online cheap
hytrin 1mg generic azulfidine generic order azulfidine 500mg without prescription
fosamax 70mg cost generic motrin order generic famotidine 20mg
zyban medication cheap quetiapine 50mg buy quetiapine online
zoloft 50mg over the counter kamagra sale purchasing viagra on the internet
order molnunat 200 mg without prescription buy lansoprazole 30mg without prescription lansoprazole 15mg usa
zoloft 50mg generic buy sertraline 50mg pill 50mg viagra
generic cialis india Real cialis for sale sildenafil generic
buy provera 10mg generic periactin 4 mg cost order periactin 4 mg for sale
order isotretinoin without prescription amoxicillin 1000mg cost purchase prednisone sale
purchase piracetam without prescription purchase viagra sale sildenafil for men over 50
azithromycin online azithromycin 250mg for sale gabapentin 600mg for sale
buy furosemide 100mg generic buy plaquenil 400mg for sale hydroxychloroquine oral
lasix 40mg generic order hydroxychloroquine for sale plaquenil 400mg us
glycomet canada purchase atorvastatin pills cialis over the counter
itraconazole over the counter itraconazole 100 mg ca tindamax 300mg sale
glucophage 500mg without prescription buy atorvastatin 20mg generic buying cialis cheap
amlodipine 5mg sale brand tadalafil 40mg cialis 20mg ca
clozapine over the counter clozapine pill dexamethasone 0,5 mg price
buy amlodipine order tadalafil 10mg sale cost cialis 10mg
order sildenafil sale buy sildenafil 50mg sale order lisinopril 2.5mg online
prilosec 20mg sale write my paper for me free poker online games
lopressor 50mg uk order levitra 20mg pill vardenafil 10mg cheap
cheap essay online playing poker online for money real money casino app
order lopressor pill order generic vardenafil 10mg buy vardenafil online
purchase essay online write my cover letter for me no deposit casino
brand clomiphene buy clomid slot machine games
buy priligy 90mg generic order misoprostol 200mcg generic synthroid 150mcg for sale
tadalafil 20mg drug brand viagra 100mg order sildenafil 100mg online cheap
orlistat pill cost diltiazem 180mg acyclovir online order
methotrexate 2.5mg pill order reglan 10mg buy reglan sale
methotrexate 5mg without prescription buy methotrexate 5mg without prescription cost reglan
buy ozobax for sale buy zanaflex pill order toradol without prescription
cozaar 50mg generic esomeprazole 20mg without prescription buy topiramate 200mg generic
baclofen 10mg canada toradol buy online order toradol 10mg generic
brand imitrex levofloxacin 500mg uk buy dutasteride pill
order zantac 150mg celecoxib 100mg generic order celecoxib 200mg online cheap
online poker free real casino slots hollywood casino online
tamsulosin for sale online order tamsulosin for sale buy aldactone online cheap
order simvastatin 20mg online valtrex 500mg canada propecia 1mg sale
order cialis 20mg online cheap order cialis 10mg online buy cipro
zocor 20mg over the counter buy valtrex sale purchase proscar generic
flagyl 200mg canada buy bactrim 480mg without prescription cheap bactrim
order ceftin 500mg pill bimatoprost order online order generic robaxin
sildenafil for sale online buy cialis 5mg online buy tadalafil
i need help with my assignment stromectol 3mg us ivermectin 12mg tablets
trazodone 100mg price buy generic aurogra 50mg order aurogra 100mg online cheap
prednisone 10mg generic buy amoxicillin online amoxicillin 1000mg ca
online roulette game ed pills cialis without prescription
azithromycin 500mg usa order gabapentin 800mg generic buy generic gabapentin
order furosemide pill buy doxycycline sale hydroxychloroquine price
order deltasone 40mg pill minipress buy online vermox 100mg pill
furosemide 100mg pill purchase furosemide online cheap order generic plaquenil 200mg
deltasone 5mg for sale mebendazole us purchase vermox online cheap
tretinoin gel for sale buy avana 100mg pills purchase avanafil
tadalafil 20mg sale buy generic tadalafil order indocin 75mg without prescription
buy lamisil pill amoxicillin 250mg cost buy trimox 250mg pill
brand lamisil buy suprax 100mg trimox tablet
biaxin 250mg usa buy meclizine 25mg pills meclizine 25mg pill
brand naprosyn 500mg cefdinir for sale prevacid 30mg sale
cheap proventil proventil 100 mcg usa cheap cipro 1000mg
montelukast pills purchase sildenafil without prescription buy sildenafil 50mg without prescription
buy montelukast 5mg sale singulair pills sildenafil 50mg us
real cialis pills online blackjack with real money slot games free
online casino real money free casino play slots
best online casino for real money online casinos real money essay helper
brand nifedipine 10mg buy aceon for sale order allegra 120mg without prescription
buy altace 10mg altace cost purchase etoricoxib pill
academic writing article order azulfidine 500 mg generic buy sulfasalazine 500 mg pill
buy ramipril 5mg without prescription purchase altace order arcoxia 60mg pill
doxycycline drug clindamycin without prescription cleocin 300mg cost
olmesartan over the counter buy benicar 20mg online cheap depakote 500mg cheap
clobetasol price buy generic buspar 5mg cheap cordarone
clobetasol cheap temovate ca order cordarone 200mg pill
amoxicillin 1000mg without prescription order generic stromectol 12mg ivermectin goodrx
fosamax 35mg pill order nitrofurantoin 100 mg pill ibuprofen 600mg sale
order priligy online cheap purchase dapoxetine online cheap motilium 10mg ca
order priligy 30mg without prescription brand avana 100mg cost domperidone 10mg
order indomethacin 50mg pill order flomax 0.4mg online cenforce generic
order doxycycline pills medrol 16mg tablets purchase medrol
order doxycycline 100mg doxycycline 100mg over the counter medrol generic name
tadalafil cost plavix 75mg us order generic trimox 250mg
free shipping cialis viagra pharmacy order viagra
minocin for sale order hytrin 1mg hytrin 5mg uk
buy tadalafil 5mg cheap generic viagra generic sildenafil 50mg
tadalafil 40mg price Best price for cialis can you buy ed pills online
buy glucophage 1000mg pill calan 120mg generic cheap nolvadex 10mg
modafinil oral buy provigil 200mg pills order promethazine 25mg generic
deltasone 10mg price prednisone 5mg sale amoxicillin brand
order clomiphene generic clomiphene online prednisolone 40mg pill
isotretinoin 10mg us prednisone 40mg price order generic ampicillin 250mg
fildena over the counter buy ed medication online proscar for sale
accutane over the counter deltasone 20mg cheap buy ampicillin 250mg for sale
ivermectin topical stromectol 6mg for sale order prednisone 10mg sale
order ondansetron pills order ondansetron 8mg without prescription sulfamethoxazole brand
buy stromectol uk ed solutions order prednisone 20mg online cheap
order accutane 10mg pill order amoxicillin generic buy zithromax 500mg generic
order asthma pills buy augmentin 625mg online augmentin 1000mg ca
purchase absorica online cheap buy amoxil online buy azithromycin 250mg generic
prednisolone 10mg drug order neurontin 100mg for sale order furosemide 40mg online
order provigil 200mg pill oral provigil 100mg metoprolol 100mg pills
generic prednisolone 5mg neurontin 800mg price order lasix 40mg online
doxycycline for sale online vardenafil pills buy acyclovir for sale
imuran 25mg cheap buy micardis 20mg pills buy naprosyn for sale
propranolol cheap generic fluconazole coreg 25mg cost