#python 3.6 #蔡军生 #http://blog.csdn.net/caimouse/article/details/51749579 # from re_test_patterns import test_patterns test_patterns( 'This is some text -- with punctuation.', [(r'^\w+', 'word at start of string'), (r'\A\w+', 'word at start of string'), (r'\w+\S*$', 'word near end of string'), (r'\w+\S*\Z', 'word near end of string'), (r'\w*t\w*', 'word containing t'), (r'\bt\w+', 't at start of word'), (r'\w+t\b', 't at end of word'), (r'\Bt\B', 't, not start or end of word')], )
原
在我们日常使用中,经常需要搜索关键位置进行字符串的匹配,比如一行文本的开头,又比如一个字符串的开头,或者结尾。 这时候就需要使用正则表达式的边界符进行匹配,它们定义如下:
定义字符 意义
^ 字符串的开头或一行的开头
$ 字符串的结尾或一行的结尾
\A 字符串的开头
\Z 字符串的结尾
\b 空字符串的开头或一个单词的结尾
\B 非空字符串的开头或非一个单词的结尾,与\b相反
测试例子如下:
结果输出如下:
'^\w+' (word at start of string)
'This is some text -- with punctuation.'
'This'
'\A\w+' (word at start of string)
'This is some text -- with punctuation.'
'This'
'\w+\S*$' (word near end of string)
'This is some text -- with punctuation.'
..........................'punctuation.'
'\w+\S*\Z' (word near end of string)
'This is some text -- with punctuation.'
..........................'punctuation.'
'\w*t\w*' (word containing t)
'This is some text -- with punctuation.'
.............'text'
.....................'with'
..........................'punctuation'
'\bt\w+' (t at start of word)
'This is some text -- with punctuation.'
.............'text'
'\w+t\b' (t at end of word)
'This is some text -- with punctuation.'
.............'text'
'\Bt\B' (t, not start or end of word)
'This is some text -- with punctuation.'
.......................'t'
..............................'t'
.................................'t'
深入浅出Numpy
五子棋游戏开发
想对作者说点什么?
我来说一句
正则表达式之字符边界
零宽与非零宽非零宽字符:能够匹配字符的(特殊)字符。如:\d会匹配一个数字,\s会匹配空白字符 零宽字符:不匹配字符,只标记位置(具体含义见下文)字符边界 表达式 表示含义 ^ 与字符...
正则表达式边界符中的 ^, $, \A, \Z, \z
转载自 http://blog.csdn.net/ggicci/article/details/8015087 Regex : 本文介绍正则表达式中边界符 ^ 和 $ 以及 \A 和 \Z...
正则表达式 —— 插入符(anchor, ^)和美元符($)
注:例子使用Python的re模块。由于插入符(^)输入和表示不是很明显,所以使用英文anchor代替。 正则表达式的符号大部分都是用来匹配字符串中单个字符的,但是 ^ 和 $不是用来匹配单...
正则基础之——\b 单词边界
1 概述 “\b”匹配单词边界,不匹配任何字符。 “\b”匹配的只是一个位置,这个位置的一侧是构成单词的字符,另一侧为非单词字符、字符串的开始或结束位置。“\b”是零宽度的。 基...
正则表达式的模式匹配符
1 \ 指出接着的字符为特殊字符。 例如:/b/匹配字符“b”,通过在b前面加一个反斜杠\,也就是/\b/,则该字符变成特殊字符,表示匹配一个单词的分界线 2 ^ 表示匹配的字符必须在最...

没有更多推荐了,返回首页