2019独角兽企业重金招聘Python工程师标准>>>

1.  References

http://docs.python.org/library/re.html#module-re

http://docs.python.org/library/re.html#raw-string-notation

http://docs.python.org/howto/regex.html#regex-howto

"mastering regular expression"

2. an example in bitbake

'(?P<base>.*?)(?P<keyword>_append|_prepend)(_(?P<add>.*))?'

This regular expression matches strings which have form 'xxx_append' or 'xxx_prepend' or 'xxx_append_yyy' or 'xxx_prepend_yyy'.

Let's take a simple analysis on it.

REGEXP = '(?P<base>.*?)(?P<keyword>_append|_prepend)(_(?P<add>.*))?' = ABC? (match AB or ABC)

A = '(?P<base>.*?)' matches the same string set with '.*?' but the matched substring could be accessed by the identifier base.

B = '(?P<keyword>_append|_prepend) matches '_append' or '_prepend'.

C = (_(?P<add>.*)) matches string like '_xxxxxxxxx'

Following the test code for this regular expression the the corresponding output in shell.

#!/usr/bin/env python                                                                                                                                                                                           # test_regexp.py regexp string
import sys
import re#pattern = sys.argv[1]
#string = sys.argv[2]                                                                                                                                                                                           def test(pattern, string):result = re.match(pattern, string)if result == None:print (pattern, string, None)else:print (pattern, string, result.group('keyword'), result.group('add'), result.group(0))pattern = '(?P<base>.*?)(?P<keyword>_append|_prepend)(_(?P<add>.*))?'
test(pattern, 'hello_append')
test(pattern, 'hello')
test(pattern, 'hello_prepend')
test(pattern, 'hello_append_add_package')
test(pattern, 'hello_append_world_package')

chenqi@chenqi-OptiPlex-760:~/mypro/python$ ./test_regexp.py
('(?P<base>.*?)(?P<keyword>_append|_prepend)(_(?P<add>.*))?', 'hello_append', '_append', None, 'hello_append')
('(?P<base>.*?)(?P<keyword>_append|_prepend)(_(?P<add>.*))?', 'hello', None)
('(?P<base>.*?)(?P<keyword>_append|_prepend)(_(?P<add>.*))?', 'hello_prepend', '_prepend', None, 'hello_prepend')
('(?P<base>.*?)(?P<keyword>_append|_prepend)(_(?P<add>.*))?', 'hello_append_add_package', '_append', 'add_package', 'hello_append_add_package')
('(?P<base>.*?)(?P<keyword>_append|_prepend)(_(?P<add>.*))?', 'hello_append_world_package', '_append', 'world_package', 'hello_append_world_package')

3. a complete list of metacharacters

. ^ $ * + ? { } [ ] \ | ( )

4. predefined special sequences

\d
Matches any decimal digit; this is equivalent to the class [0-9].
\D
Matches any non-digit character; this is equivalent to the class [^0-9].
\s
Matches any whitespace character; this is equivalent to the class [ \t\n\r\f\v].
\S
Matches any non-whitespace character; this is equivalent to the class [^ \t\n\r\f\v].
\w
Matches any alphanumeric character; this is equivalent to the class [a-zA-Z0-9_].
\W

Matches any non-alphanumeric character; this is equivalent to the class[^a-zA-Z0-9_].

To Be Continue ...

转载于:https://my.oschina.net/u/158589/blog/71888

Python Regular Expression相关推荐

  1. Python正则表达式(regular expression)简介-re模块

    Python正则表达式(regular expression)简介-re模块 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 就其本质而言,正则表达式(或RE模块)是一种小型的,高度 ...

  2. 21天学Python --- 打卡2:Regular Expression

    21天学Python --- 打卡2:Regular Expression 1.Common characters 2.Priority 3.Demo 3.1 普通字符 3.2 中括号 3.3 首字母 ...

  3. python 正则表达式(Regular Expression)基础学习笔记

    python 正则表达式(Regular Expression) 正则表达式基础 search():只返回第一个匹配的字符串 findall():将搜寻结果以列表方式返回 import re # 导入 ...

  4. 【论文阅读】ReDoSHunter: A Combined Static and Dynamic Approach for Regular Expression DoS Detection

    TODO 引文信息 [1] LI Y, CHEN Z, CAO J, 等. ReDoSHunter: A Combined Static and Dynamic Approach for Regula ...

  5. Regular Expression Matching

    正则匹配 Regular Expression Matching Implement regular expression matching with support for '.' and '*'. ...

  6. java正则表达式及api_JAVA常用API:正则表达式regular expression

    一.正则表达式的概念 正则表达式,regular expression,在代码中通常简写成regex 正则表达式是一个字符串,使用每单个字符串来描述.定义匹配规则,匹配一系列符合某个语法规则的字符串. ...

  7. leetcode 10 Regular Expression Matching

    题目连接 https://leetcode.com/problems/regular-expression-matching/ Regular Expression Matching Descript ...

  8. Reflection,Regular Expression,Threading,IO,AppDomain,Web Service/Remoting Service,ORM

    Reflection,Regular Expression,Threading,IO,AppDomain,Web Service/Remoting Service,ORM 先开个头,慢慢完善! Ref ...

  9. 【Nginx】错误: [emerg] “proxy_pass“ cannot have URI part in location given by regular expression,...

    前言 nginx 1.15.11(下面的代码在此版本测试通过) win10 phpstudy 8.1.1.3 错误 nginx: [emerg] "proxy_pass" cann ...

  10. Search Engine —— Regular Expression(Spider)

    Regular Expression,即正则表达式:用来查找符合某些负责规则的字符串的需要.它真是用于描述这些规则的工具. 1. \b 是一个元字符,用来匹配一个位置,代表着单词的开头或结尾,也就是单 ...

最新文章

  1. GDALWarp设置GDALWarpOptions::dfWarpMemoryLimit过大时处理失败
  2. oracle 更改实便例名称,大家好,请教在oracle中能否获取update记录 所涉及的字段的名称?如能实现的话,请教相关实现方法。谢谢~...
  3. Temporal Segment Networks(TSN)实验及错误日志
  4. python参数估计_用python求参数估计的置信区间
  5. launchpad乐器_请把《明日之子》里徐洋称为哆啦A洋,还有什么乐器是你不会的?...
  6. javaweb基础知识点记录1
  7. pycharm pip安装_pycharm无法调用pip安装的第三方库
  8. NFA转DFA程序设计
  9. Zabbix---1 监控主机磁盘空间
  10. mybatis查询mysql数据库很慢_mybatis查询大量数据库
  11. 一维卷积神经网络_人人都能看得懂的卷积神经网络——入门篇
  12. RSA加密解密及RSA加签验签
  13. php如何解决高并发问题
  14. Java 测试:将用户输入的字符串中的敏感字和谐成星星
  15. 变电所、分区所、AT所
  16. 大数据:互联网大数据和物联网大数据有何不同?
  17. mac 安装win7 攻略
  18. 商品规格表的制作 mysql_商品规格设计
  19. 【面经】2021 中国农业银行 笔试编程题
  20. raid管理软件MegaRAID Storage Manager(MSM)的安装和使用

热门文章

  1. java 读取文件的大小_java读取文件大小
  2. python 图片 变清晰_python模糊图片过滤的方法
  3. 用联发科芯片的手机能升级鸿蒙吗,华为鸿蒙系统降临!首批升级手机确定,联发科芯片被放弃?...
  4. 建模算法(五)——图与网络
  5. Vue2.0七——生命周期
  6. 2个比较经典的PHP加密解密函数分享
  7. Android课程---如何用网格视图做出手机桌面APP
  8. WPF入门教程系列二十——ListView示例(二)
  9. JS Date.Format
  10. Centos查看进程命令