这里是一个php正则表达式示例的列表.也许这有助于某人,正如管理员/或其他用户不清楚,我试图分享我的方法.
preg_match进行搜索(preg_replace是一个替换器).
preg_match有三个参数--preg_match(FindWhat,FindWhere,GivingOutput);
例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:
@
Run Code Online (Sandbox Code Playgroud)
preg_match只找到一个结果(第一个找到的结果),有两个选项:[0]或[1].
例2):在我们的搜索条件中找到所有内容(任何字符,单词..):
<?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
Run Code Online (Sandbox Code Playgroud)
(注意,如果您在搜索条件中不使用括号(如上所述,在示例1中),那么选项[1]是不可用的
例3):如果您的目标文本有很多相同的出现,如下所示:$ text ='hello用户Jimmy Jones,我的.你好用户Mery Pawders,它还是我.';
现在,这里有两个不同的匹配,所以,我们需要使用preg_match_all
<?php
$text='hello user Jimmy Jones, its me. hello user Mery …Run Code Online (Sandbox Code Playgroud) 我正在使用这个正则表达式来获得一个数字.
Regex.Replace(foo, "[^.0-9]", "")
Run Code Online (Sandbox Code Playgroud)
如何让它不删除空格?