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函数)的更多相关文章

  1. Python正则表达式Regular Expression基本用法

    资料来源:http://blog.csdn.net/whycadi/article/details/2011046   直接从网上资料转载过来,作为自己的参考.这个写的很清楚.先拿来看看. 1.正则表 ...

  2. lc面试准备:Regular Expression Matching

    1 题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...

  3. Regular Expression Syntax

    python的正则表达式 正则表达式的概念 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个"规则字符串",这个"规 ...

  4. 【一天一道LeetCode】#10. Regular Expression Matching

    一天一道LeetCode系列 (一)题目 Implement regular expression matching with support for '.' and '*'. '.' Matches ...

  5. python(4): regular expression正则表达式/re库/爬虫基础

    python 获取网络数据也很方便 抓取 requests 第三方库适合做中小型网络爬虫的开发, 大型的爬虫需要用到 scrapy 框架 解析 BeautifulSoup 库, re 模块 (一) r ...

  6. Python 模块 re (Regular Expression)

    使用 Python 模块 re 实现解析小工具   概要 在开发过程中发现,Python 模块 re(Regular Expression)是一个很有价值并且非常强大的文本解析工具,因而想要分享一下此 ...

  7. Leetcode Week1 Regular Expression Matching

    Question Given an input string (s) and a pattern (p), implement regular expression matching with sup ...

  8. [LeetCode] Regular Expression Matching 正则表达式匹配

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  9. myeclipse中导入js报如下错误Syntax error on token "Invalid Regular Expression Options", no accurate correc

    今天在使用bootstrap的时候引入的js文件出现错误Syntax error on token "Invalid Regular Expression Options", no ...

随机推荐

  1. Windows平台下主要的内存管理途径

    new / delete   malloc / free    CoTaskMemAlloc / CoTaskMemFree    IMalloc::alloc / IMalloc/free    G ...

  2. COALESCE函数

    --SQL学习笔记一 --函数coalesce --功能返回参数中第一个非NULL值 --语法 COALESCE ( expression [ ,n ] ) --创建测试表 IF OBJECT_ID( ...

  3. Java中的不可变类

    概念:不可变类的意思是创建该类的实例后,该实例的属性是不可改变的.java中的8个包装类和String类都是不可变类.所以不可变类并不是指该类是被final修饰的,而是指该类的属性是被final修饰的 ...

  4. C++ —— 编译程序

    目录: 0.GCC online documentation 1.gcc编译器 常用命令 2.VC编译器  常用参数说明 3.C预处理器命令说明 4.debug 和 release 的区别 0.GCC ...

  5. Windows平台下,Scrapy Installation,安装问题解决

    按理说直接:pip install scrapy 就可以成功,但是出现了错误"libxml/xpath.h: No such file or directory" "er ...

  6. Resizable 2th click not working

    here's a simple solution.  just destroy the resizable function, then rebuild it. try { $("#div& ...

  7. Reachability下载地址

    https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html

  8. SpringMVC整合fastjson-1.1.41

    以前用fastjson也只是硬编码,就好像这篇博文写的http://blog.csdn.net/jadyer/article/details/24395015 昨天心血来潮突然想和SpringMVC整 ...

  9. paip.gch预编译头不生效的原因以及解决:

    paip.gch预编译头不生效的原因以及解决: 作者Attilax ,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/a ...

  10. LeetCode——Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...