<?php

    //2.正则表达式:元字符
    $model = "/php/";
    $string = "php";

    // 1.元字符 [a-z] 匹配任何a-z的字符串
    // []:中括号语法表示,匹配任意一个即可
    $model = "/[abc]/";
    $string = "a";
    echo preg_match($model, $string);

    $model = "/[abc]/";
    $string = "b";
    echo preg_match($model, $string);

    $model = "/[abc]/";
    $string = "a";
    echo preg_match($model, $string);

    $model = "/[abc]/";
    $string = "abdddsssaassas";
    echo preg_match($model, $string);

    $model = "/[abc]/";
    $string = "dddddddasssddddd";
    echo preg_match($model, $string);

    $model = "/[0-9]{3}/";
    $string = "123ffssd";
    echo preg_match($model, $string);

    //备注:[]匹配该范围内的任意一个字符即可

    $model = "/[a-zA-Z0-9_]/";//
    $string = "_";
    echo preg_match($model, $string)."<hr>";

    //2.[^abc]:主意是在中括号内部加^
    //[^abc]表示匹配除了abc,任何一个就可以了
    $model = "/[^abc]/";
    $string = "a";
    echo preg_match($model, $string);

    $model = "/[^abc]/";
    $string = "b";
    echo preg_match($model, $string);

    $model = "/[^abc]/";
    $string = "c";
    echo preg_match($model, $string);

    $model = "/[^abc]/";
    $string = "abcsabc";
    echo preg_match($model, $string)."<hr>";

    //3. \w 匹配任何包含a-zA-Z0-9和下划线的字符串
    // \w <=> [a-zA-Z0-9_]
    $model = "/\w/";
    $string = "c";
    echo preg_match($model, $string);

    $model = "/\w/";
    $string = "&";
    echo preg_match($model, $string);

    $model = "/\w/";
    $string = "0&";
    echo preg_match($model, $string)."<hr>";

    //4. \W(与小写的要区分)
    // \W 与 \w 正好相反
    $model = "/\W/";
    $string = "&";
    echo preg_match($model, $string);

    $model = "/\W/";
    $string = "0&";
    echo preg_match($model, $string);

    $model = "/\W/";
    $string = "s";
    echo preg_match($model, $string)."<hr>";

    //05. \d 匹配任意数字
    // 与 [0-9]相同
    $model = "/\d/";
    $string = "11";
    echo preg_match($model, $string);

    $model = "/\d/";
    $string = "$";
    echo preg_match($model, $string);

    $model = "/\d/";
    $string = "AS";
    echo preg_match($model, $string)."<HR>";

    //06:\D 与 \d 相反
    // 与 [^0-9]相同
    $model = "/\D/";
    $string = "$0";
    echo preg_match($model, $string);

    $model = "/\D/";
    $string = "0";
    echo preg_match($model, $string)."<hr>";

    //07:\s 匹配任何空白字符
    $model = "/\s/";
    $string = "$0";
    echo preg_match($model, $string);

    $model = "/\s/";
    $string = " 0";
    echo preg_match($model, $string)."<hr>";

    // \S 与 \s 相反
    $model = "/\S/";
    $string = " 0";
    echo preg_match($model, $string)."<hr>";

    //08: \b 表示到达了单词的边界,边界可以理解为空格
    // \B 表示没有到达空格
    $model = "/php\b/";
    $string = "php";
    echo preg_match($model, $string);

    $model = "/php\b/";
    $string = "php,";
    echo preg_match($model, $string);

    $model = "/php\b/";
    $string = "phpzzz";
    echo preg_match($model, $string);

    $model = "/php\b/";
    $string = "php,gggggg";
    echo preg_match($model, $string)."<hr>";

    //09: \匹配正则中的特殊字符
    // 比如+是正则里面的特殊字符,如果要匹配特殊字符的话,必须加上 \
    $model = "/ph\+p/";
    $string = "ph+p";
    echo preg_match($model, $string);

    $model = "/ph\+p/";
    $string = "ccccph+pdddd";
    echo preg_match($model, $string)."<hr>";
?>

php基础31:正则匹配-元字符的更多相关文章

  1. Java基础--常用正则匹配符号(必背,必须背,死都要背)

    1.字母:匹配单个字母 (1)A:表示匹配字母A: (2)\\:匹配转义字符“\”: (3)\t:匹配转义字符“\t”: (4)\n:匹配转义字符“\n”: 2.一组字符:任意匹配里面的一个单个字符: ...

  2. Python 基础之正则之二 匹配分组,正则相关函数及表达式修饰符

    四.匹配分组   [元字符] 分组符号 a|b   匹配字符a 或 字符b  (如果两个当中有重合部分,把更长的那个放前面) (ab)   匹配括号内的表达式 ,将()作为一个分组 num  引用分组 ...

  3. Flask基础(3):session、flash、特殊装饰器、蓝图、路由正则匹配、上下文管理 & flask-session

    Session: Flask 默认将 session 以加密的形式放到了浏览器的 cookie 中 Flask 的 session 就是一个字典,字典有什么方法 session 就有什么方法 flas ...

  4. python中正则匹配之re模块

    Python中正则表达式 re:re是提供正则表达式匹配操作的模块 一.什么是正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某个模式匹配,Python 自1.5版本起 ...

  5. MySQL全面瓦解8:查询的正则匹配

    概述 上一章 查询的过滤条件,我们了解了MySQL可以通过 like % 通配符来进行模糊匹配.同样的,它也支持其他正则表达式的匹配,我们在MySQL中使用 REGEXP 操作符来进行正则表达式匹配. ...

  6. Aho-Corasick算法、多模正则匹配、Snort入门学习

    希望解决的问题 . 在一些高流量.高IO的WAF中,是如何对规则库(POST.GET)中的字符串进行多正则匹配的,是单条轮询执行,还是多模式并发执行 . Snort是怎么组织.匹配高达上千条的正则规则 ...

  7. python_way day18 html-day4, Django路由,(正则匹配页码,包含自开发分页功能), 模板, Model(jDango-ORM) : SQLite,数据库时间字段插入的方法

    python_way day18 html-day4 1.Django-路由系统   - 自开发分页功能 2.模板语言:之母板的使用 3.SQLite:model(jDango-ORM) 数据库时间字 ...

  8. java 正则匹配空格字符串 正则表达式截取字符串

    java 正则匹配空格字符串 正则表达式截取字符串 需求:从一堆sql中取出某些特定字符串: 比如配置的sql语句为:"company_code = @cc and project_id = ...

  9. 常用的JavaScript正则匹配规则代码收藏,很实用

    收集一些常用的JavaScript正则表达式匹配规则,比如匹配电话号码.Email.中文字符.身份证号.邮编.QQ号.过滤空白行.匹配特定数字等.觉得这玩意是很有用的,只不过自己水平菜,老是自己写不出 ...

随机推荐

  1. iOS之小功能模块--彩虹动画进度条学习和自主封装改进

    前言: 首先展示一下这个iOS小示例的彩色进度条动画效果: 阅读本文先说说好处:对于基础不好的读者,可以直接阅读文末尾的"如何使用彩虹动画进度条"章节,然后将我封装好的这个功能模块 ...

  2. eval 为什么加括号

    <!DOCTYPE html> <html> <head> <title>eval学习</title> <script type=&q ...

  3. Atitit. 软件设计 模式 变量 方法 命名最佳实践 vp820 attilax总结命名表大全

    Atitit. 软件设计 模式 变量 方法 命名最佳实践 vp820 attilax总结命名表大全 1. #====提升抽象层次1 2. #----使用通用单词1 3. #===使用术语..1 4.  ...

  4. JavaScript Patterns 3.2 Custom Constructor Functions

    When you invoke the constructor function with new, the following happens inside the function: • An e ...

  5. MongoDB学习笔记——聚合操作之MapReduce

    MapReduce MongoDB中的MapReduce相当于关系数据库中的group by.使用MapReduce要实现两个函数Map和Reduce函数.Map函数调用emit(key,value) ...

  6. PL/SQL之--函数

    一.函数 函数是作为数据库对象存储在oracle数据库中,函数又被称为PL/SQL子程序.oracle处理使用系统提供的函数之外,用户还可以自己定义函数.函数通常被作为一个表达式来调用或存储过程的一个 ...

  7. Android开发之 adb 启动问题或是部署应用不成功,出现“The connection to adb is down, and a severe error has occured.”错误

    首先是今天想测试下应用,没有问题的话就进行下一步的操作来着,结果遇到这个问题, The connection to adb is down, and a severe error has occure ...

  8. Python2

    安装pycharm专业版,不要汉化 要想写的代码支持linux和2.0版本需要在开头加上注释 #/usr/bin/env python #-*- coding:utf-8 -*- 运算符 结果是值 算 ...

  9. 09_Sum游戏(UVa 10891 Game of Sum)

    问题来源:刘汝佳<算法竞赛入门经典--训练指南> P67 例题28: 问题描述:有一个长度为n的整数序列,两个游戏者A和B轮流取数,A先取,每次可以从左端或者右端取一个或多个数,但不能两端 ...

  10. ipc之消息队列

    消息队列以链表的方式将消息存储于内核中,调用msgsnd,msgrcv函数往消息队列里面投送,取出指定的消息. 创建一个消息队列 生成一个消息队列或者获取已有消息队列id #include <s ...