正则表达式和匹配 [英] Regular Expressions and Matching

查看:164
本文介绍了正则表达式和匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是php正则表达式示例的例子列表。也许这有助于某人,因为管理员/或其他用户不清楚,我正在尝试分享我的方法。



preg_match执行搜索(preg_replace是一个替代)。 _
preg_match有三个参数 - preg_match(FindWhat,FindWhere,GivingOutput);



示例1)

 <?php 
//所有期望的字母和数字
$ text ='abc345fg @ h'
$ newfilename = preg_match('/ [^ a-zA-Z0-9。] /',$ text,$ out);
echo $ out [0];
?>
的输出将是:
@

preg_match只找到一个结果发现结果),有两个选项:[0]或[1]。



示例2):查找所有内容(任何字符,单词。 。)在我们的搜索条件内:

 <?php 
$ text ='abcdefghijklmnopqrst'
$ newfilename = preg_match('/ ij(。*?)mn /',$ text,$ out);
echo $ out [0];
echo $ out [1];
?>
[1] - 只有内部搜索结果(我们在括号中有ij和mn之间)):
kl

[0]整个搜索结果:
ijklmn

(注意,该选项[1]不可用如果在搜索条件中不要使用括号(如上所述,在示例1中)



示例3)
if您的目标文字有许多相同的情况,像这样:
$ text ='hello user 吉米琼斯,它的我。现在,这里有两个不同的比赛,所以我们需要使用preg_match_all



p>

 <?php 
$ text ='hello user Jimmy Jones,it me。你好,用户Mery Pawders,它的我。
$ newfilename = preg_match_all('/ hello user(。*?)its /',$ text,$ out);
foreach($ out [1] as $ found_one){echo $ found_one;}
//或使用$ out [0]进行完全搜索匹配
?>

输出将为:
吉米琼斯,
Mery Pawders,

示例4):搜索许多可能性:

 <?php 
$ text ='member ACCOUNT7';
preg_match(/ ACCOUNT [123456789] /,$ text,$ out);
echo $ out [1];
?>

输出将为:
ACCOUNT7

示例5):要找到一个字符串,而输入的文本包含新行,则必须在结尾使用** s **;

 code><?php 
$ text ='one
two
three';
preg_match(/ one(。*?)three / s,$ text,$ out);
echo $ out [1];
?>

输出将为:
两个

示例6):您的搜索始终区分大小写。要进行案例INSENSITIVE搜索,请在末尾使用 i (如果需要,不使用 );

 <?php 
$ text ='ONE TWO TREE';
preg_match(/ one(。*?)three / si,$ text,$ out);
echo $ out [1];
?>

示例7):搜索特殊字符(如/\".<*'等等)在preg_match中,您需要使用这个转义符号:\

 <?php 
$ text ='hello Jimmy / Kroger';
preg_match(/ Jimmy \ / Kroger /,$ text,$ out);
echo $ out [0];
?>

现在,我们可以使用^



示例8):查找所有内容而不是字母和数字:

 <?php 
$ text ='abc @ *& ^)($%';
preg_match_all('/ [^ a- zA-Z0-9。] /',$ text,$ out);
foreach($ out [0] as $ varr){echo $ varr;}
?>
输出将是:
@ *& ^)($%

for 搜索和REPLACE ,我们有一个不同的结构,因为我们需要使用新的变量。



示例9):find并替换每一个使用此运算符的其他字符的字母和数字而不是:^

 <?php 
$ text ='ab2sq)(& *(%$%^ $ @%n23f9';
$ variable = preg_replace('/ [^ a-zA-Z0-9。] /','a',$ text);
echo $ variable;
?>
输出将为:
ab2sqn23f9

示例10) strong>:搜索并添加发现的资料内的东西:

 <?php 
$ text =来自奥地利的Niko
$ variable = preg_replace('/(Niko。*?)from /','$ 1 Gomez',$ text);
echo $ variable;
?>
的输出将是:

它的我,Niko Gomez奥地利

示例11):查找文本内的所有链接:

 <?php 
$ text ='我的网站是http://example.com,在我的网页上,http://example.com/page37/blabla.html我写了一些东西..'
preg_match_all(/ [[:alpha:]] +:\ / \ / [^<> [:space:]] + [[:alnum:] \ /] /文字,$出);
foreach($ out [0] as $ varr){echo $ varr;}
?>
输出将为:
http://example.com
http://example.com/page37/blabla.html

示例12):像示例11(但带有替换) - 查找文本中的链接并将其放在锚定的标签中:

 <?php 
$ text ='我的网站是http://example.com,在我的页面上,在http://example.com/page37/trid.html我写了一些东西..'
$ variable = preg_replace(/ [[:alpha:]] +:\ / \ / [^<> [:space:]] + [[:alnum:] \ /] / ,'< a href =\\0> \\0< / a>',$ text);
echo $ variable;
?>

输出将是同一句,但链接将被锚定。



1)提示:如果只想检查一个字符串是否包含在另一个字符串中,请不要使用preg_match()。使用stristr()或strpos(),因为它们会更快。



2)**更高级的,具体的php正则表达式示例,使用谷歌,或参见**
FULL OPTIONS AND MANUALS at - http://www.php.net/manual/en/reference.pcre.pattern.syntax.php



(您可以稍后查看所有运营商列表这里 -

http:/ www p p p p /php-regular-expressions.htmlrel =noreferrer> http://www.noupe.com/php/php-regular-expressions.html $



3)对于hmtl代码,存在特殊的光,php软,称为Dom Parser。但是有时候,如果你知道php正则表达式,你可能不需要dom解析器。

解决方案

我假设,当你说 $ email,这意味着 @ 标志之前的内容。在这种情况下,您可以使用这个正则表达式:

  $ email ='SHOP_psgarden_0@somewhere.com'; 
if(preg_match('/ ^ shop.*0@/i',$ email)=== 1){
echo'Yes!';
}

您还可以使用正常的程序代码检查:

  $ email ='SHOP_psgarden_0@somewhere.com'; 
$ local = substr($ email,0,strpos($ email,'@'));
$ amalgam = substr($ local,0,4)。 substr($ local,-1);
if(strcasecmp('shop0',$ amalgam)=== 0){
echoyes;
} else {
echono;
}


here is a list of examples of php regular expressions examples. maybe this helps someone,as admin/ or another user cant make clear, that i was trying to share my approaches.

preg_match does the search (preg_replace is a replacer).
preg_match has three parameters - preg_match(FindWhat, FindWhere, GivingOutput);

example 1):

<?php
//everything expect letters and numbers
$text='abc345fg@h';
$newfilename=preg_match('/[^a-zA-Z0-9.]/',$text, $out);
echo $out[0];
?>
output will be:
@

preg_match finds only one result (the firstly found result), with two options: [0] or [1].

example 2): find everything (any characters,words..) inside our search criteria:

<?php
$text='abcdefghijklmnopqrst';
$newfilename=preg_match('/ij(.*?)mn/',$text, $out);
echo $out[0];
echo $out[1];
?>
[1] -gives only the inner search result (what we had in the brackets,  between "ij" and "mn"):
kl 

[0] -gives the whole search result:
ijklmn

(Note, that option [1] is not avaiable if when you dont use brackets in search criteria (as we have above, in example 1)

example 3): if your target text has many same occurences, like this: $text='hello user Jimmy Jones, its me. hello user Mery Pawders, its still me.';

now, here are two different matches, so, we need to use preg_match_all

<?php
$text='hello user Jimmy Jones, its me. hello user Mery Pawders, its me.';
$newfilename=preg_match_all('/hello user (.*?) its/',$text, $out);
foreach ($out[1] as $found_one) {echo $found_one;}
//or use $out[0] for full search match
?>

output will be:
Jimmy Jones,
Mery Pawders,

example 4): search among many possibilities:

<?php
$text = 'member ACCOUNT7';
preg_match("/ACCOUNT[123456789]/",$text,$out);
echo $out[1];
?>

output will be:
ACCOUNT7

example 5): To find a string, while input text contains new lines, you must use**s** at the end;

<?php
$text = 'one
two
three';
preg_match("/one(.*?)three/s",$text,$out);
echo $out[1];
?>

output will be:
two

example 6): Your search is always case sensitive. To make a case INSENSITIVE search, use i at the end (if you want, without s);

<?php
$text = 'ONE TWO TREE';
preg_match("/one(.*?)three/si",$text,$out);
echo $out[1];
?>

example 7): to search for special characters (like /".<*'? and etc..) inside preg_match, you need to use this escape sign: \

<?php
$text = 'hello Jimmy/Kroger ';
preg_match("/Jimmy\/Kroger/",$text,$out);
echo $out[0];
?>

now, we can use ^ operator, which searches for results conversely.

example 8): find everything rather than letters and numbers:

<?php
$text = 'abc@*&^)($%';  
preg_match_all('/[^a-zA-Z0-9.]/',$text,$out);
foreach ($out[0] as $varr) {echo $varr;}
?>
output will be:
@*&^)($%

for SEARCH and REPLACE, we have a bit different structure, as we need to use the new variable.

example 9): find and replace everything rather than letters and numbers with other character, using this operator: ^

<?php
$text = 'ab2sq)(&*(%$%^$@%n23f9';   
$variable = preg_replace('/[^a-zA-Z0-9.]/','a',$text);
echo $variable;
?>
output will be:
ab2sqn23f9

example 10): search and add something inside the found resuls:

<?php
$text = 'Hi, its me, Niko from Austria';    
$variable = preg_replace('/(Niko.*?) from/', '$1 Gomez', $text); 
echo $variable;
?>
output will be :

its me, Niko Gomez Austria

example 11): find all links inside text:

<?php
$text = 'hi, my site is http://example.com, and on my page, at http://example.com/page37/blabla.html i wrote something..';  
preg_match_all("/[[:alpha:]]+:\/\/[^<>[:space:]]+[[:alnum:]\/]/",$text, $out);
foreach($out[0] as $varr){echo $varr;}
?>  
output will be:
http://example.com
http://example.com/page37/blabla.html

example 12): like the example 11 (but with replace) - find links in text and put them in anchored tags:

<?php
$text = 'hi, my site is http://example.com, and on my page, at http://example.com/page37/trid.html i wrote something..';    
$variable = preg_replace("/[[:alpha:]]+:\/\/[^<>[:space:]]+[[:alnum:]\/]/",'<a href="\\0">\\0</a>', $text);
echo $variable;
?>

output will be the same sentence, but the links will be anchored.

1) Tips: Do not use preg_match() if you only want to check if one string is contained in another string. Use stristr() or strpos() instead as they will be faster.

2) **more advanced, specific examples about php regular expressions, use google, or see ** FULL OPTIONS AND MANUALS at - http://www.php.net/manual/en/reference.pcre.pattern.syntax.php

( You can review shortly all operators list here -
http://www.catswhocode.com/blog/15-php-regular-expressions-for-web-developers
http://www.noupe.com/php/php-regular-expressions.html
)

3) for hmtl codes, there exist special light, php soft, called- Dom Parser. but sometimes, if you know php regular expressions well, you might not need dom parser.

解决方案

I assume, when you say "$email", that you mean the stuff before the @ sign. In that case you can use this regex:

$email = 'SHOP_psgarden_0@somewhere.com';
if (preg_match('/^shop.*0@/i', $email) === 1) {
    echo 'Yes!';
}

You can also check using normal procedural code:

$email   = 'SHOP_psgarden_0@somewhere.com';
$local   = substr($email, 0, strpos($email, '@'));
$amalgam = substr($local, 0, 4) . substr($local, -1);
if (strcasecmp('shop0', $amalgam) === 0) {
    echo "yes";
} else {
    echo "no";
}

这篇关于正则表达式和匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆