regular expression (php函数)
1. 正则表达式是一种字符串搜索和匹配的工具
2. php中常用正则表达式函数
- preg_match($pattern, $subject)
- preg_match_all($pattern, $subject, array &$matches)
- preg_replace($pattern, $replacement, $subject)
- preg_filter($pattern, $replacement, $subject)
- preg_grep($pattern, array $input)
- preg_split($pattern, $subject)
- preg_quote($str)
3. php函数说明
$pattern = 正则表达式
$subject = 匹配的目标函数
(1) preg_match() 和 preg_match_all() : return 匹配到结果的次数
- preg_match($pattern, $subject, [array &$matches]) : 只匹配一次, 结果为0或者1, 第三个参数可不写, 第三个参数表示地址的引用
- preg_match($pattern, $subject, array &$matches) : 匹配全部, 结果为0,1,2......
eg:
$pattern='/[0-9]/';
$subject = 'weuyr3ui76as83s0ck9';
$m1 = $m2 = array();
t1 = grep_match($pattern, $subject, $m1);
t2 = grep_match_all($pattern, $subject, $m2);
结果: m1 = array([0]=>3)
m2 = array([0]=>array([0]=>3,[1]=>7,[2]=>6,[3]=>8,[4]=>3,[5]=>0,[6]=>9))
t1 = 1
t2 = 7
(2) preg_replace 与 preg_filter : 支持数组替换
- preg_replace($pattern, $replacement, $subject) : 保留发生替换和没发生替换的值
- preg_filter($pattern, $replacement, $subject) : 保留发生替换的值
eg one:
$pattern='/[0-9]/';
$subject = 'weuyr3ui76as83s0ck9';
$replacement = '盈';
$str1 = preg_replace($pattern, $replacement, $subject);
$str2 = preg_filter($pattern, $replacement, $subject);
结果:
$str1 = 'weuyr盈ui盈盈as盈盈s盈ck盈'
$str2 = 'weuyr盈ui盈盈as盈盈s盈ck盈'
eg two:
$pattern = array('/[0123]/', '/[456]/', '/[789]/')
$replacement = array('啊', '啦', '嗦')
结果:
$str1 = 'weuyr啊ui嗦啦as嗦啊s啊ck嗦'
$str2 = 'weuyr啊ui嗦啦as嗦啊s啊ck嗦'
eg three:
$subject = array('weuy', 'r3ui', '76as83', 's', '0ck9');
结果:
$str1 = array([0]=>weuy, [1]=>r啊ui, [2]=>嗦啦as嗦啊, [3]=>s, [4]=>啊ck嗦)
$str2 = array([1]=>r啊ui, [2]=>嗦啦as嗦啊, [4]=>啊ck嗦)
(3) grep_grep($pattern, array $input) : 阉割版的grep_filter(), 只做匹配, 不做替换
eg:
$pattern='/[0-9]/';
$subject = array('weuy', 'r3ui', '76as83', 's', '0ck9');
$arr = preg_grep($pattern, $subject);
结果:
$arr = array([1]=>r3ui, [2]=>76as83, [4]=>0ck9)
(4) grep_split($pattern, $subject) : explode是该函数的子集
eg:
$pattern = '/[0-9]/';
$subject = '你2好3啊!'
$arr = preg_split($pattern, $subject);
结果:
$arr = ([0]=>你, [1]=>好, [2]=>啊!)
(5) grep_quote($str) : 正则运算符转义
eg:
$str = 'asgs{kkk}[123]'
$str = grep_quote($str)
结果:
asgs\{kkk\}\[123\]
regular expression (php函数)的更多相关文章
- Python正则表达式Regular Expression基本用法
资料来源:http://blog.csdn.net/whycadi/article/details/2011046 直接从网上资料转载过来,作为自己的参考.这个写的很清楚.先拿来看看. 1.正则表 ...
- lc面试准备:Regular Expression Matching
1 题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...
- Regular Expression Syntax
python的正则表达式 正则表达式的概念 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个"规则字符串",这个"规 ...
- 【一天一道LeetCode】#10. Regular Expression Matching
一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...
- python(4): regular expression正则表达式/re库/爬虫基础
python 获取网络数据也很方便 抓取 requests 第三方库适合做中小型网络爬虫的开发, 大型的爬虫需要用到 scrapy 框架 解析 BeautifulSoup 库, re 模块 (一) r ...
- Python 模块 re (Regular Expression)
使用 Python 模块 re 实现解析小工具 概要 在开发过程中发现,Python 模块 re(Regular Expression)是一个很有价值并且非常强大的文本解析工具,因而想要分享一下此 ...
- Leetcode Week1 Regular Expression Matching
Question Given an input string (s) and a pattern (p), implement regular expression matching with sup ...
- [LeetCode] Regular Expression Matching 正则表达式匹配
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- myeclipse中导入js报如下错误Syntax error on token "Invalid Regular Expression Options", no accurate correc
今天在使用bootstrap的时候引入的js文件出现错误Syntax error on token "Invalid Regular Expression Options", no ...
随机推荐
- Cracking the coding interview--Q2.3
Implement an algorithm to delete a node in the middle of a singly linked list,given only access to t ...
- VMware 11安装Mac OS X 10.10
http://jingyan.baidu.com/article/ff411625b9011212e48237b4.html
- 【转】vim 修改tab为四个空格
原文网址:http://blog.sina.com.cn/s/blog_620ccfbf01010erz.html 为了vim更好的支持python写代码,修改tab默认4个空格有两种设置方法: 1. ...
- jqGrid中多选
原文地址;http://www.cnblogs.com/josechuanmin/archive/2013/05/19/3087138.html 在jqGrid中设置multiselect: true ...
- 《SDN核心技术剖析和实战指南》3.1控制器核心技术读书笔记
在SDN的架构中,控制器可以说是SDN的核心,它负责对底层转发设备的控制以及向上层应用提供可编程性的北向接口.从实现上看,主要分三个层面来考虑,南向接口技术,北向接口技术以及东西向的可扩展性能力.下面 ...
- python_Opencv_处理鼠标事件
流程: 首先,创建一个鼠标事件回调函数,当鼠标事件发生时就会被执行. 鼠标事件可以是鼠标上的任何动作,比如左键按下,左键松开,左键双击等. 我们可以通过鼠标事件获得与鼠标对应的图片上的坐标. 根据这些 ...
- VS2008LINK : fatal error LNK1000: Internal error during IncrBuildImage
修改链接选项,方法:项目->属性->链接器->常规 下面的“启用增量链接”,将“是(/INCREMENTAL)”改为“否(/INCREMENTAL:NO)”,然后确定就可以了.
- intent的startActivityForResult()方法
/******************************************************************************************** * au ...
- C primer plus 读书笔记第八章
本章的标题是字符输入/输出和输入确认.主要内容是讨论用于I/O的标准函数. 1.getchar()和putchar() 这两个函数之前用过,我们通过这两个函数来讨论下缓冲区. #include &qu ...
- Mysql事务及行级锁的理解
在最近的开发中,碰到一个需求签到,每个用户每天只能签到一次,那么怎么去判断某个用户当天是否签到呢?因为当属表设计的时候,每个用户签到一次,即向表中插入一条记录,根据记录的数量和时间来判断用户当天是否签 ...