羔羊跪乳是什么意思| yuri是什么意思| 腰间盘突出有什么症状| 葡萄糖什么意思| 间接胆红素高是什么原因| 肉是什么结构的字| 真丝丝绒是什么面料| ct值是什么意思| 纤维蛋白是什么| 龋齿是什么样子的图片| 犹太人说什么语言| 瘦西湖为什么叫瘦西湖| 肺炎吃什么药| 小孩手指脱皮是什么原因| 77岁属什么生肖| 安全生产职责是什么| 突然抽搐是什么原因| 周岁什么意思| 枸杞加红枣泡水喝有什么功效| 感冒咳嗽挂什么科| 双数是什么| 骨质增生吃什么药好| 葡萄球菌感染是什么原因引起的| 甲功五项是什么意思| 什么是命运| 不想睡觉是什么原因| 79年属羊的是什么命| 溢于言表是什么意思| 149是什么意思| 1956年属什么生肖| 白膜是什么东西| 五行属什么怎么看| 晚上十一点半是什么时辰| 拈花一笑什么意思| 做三明治需要什么材料| 加鸡腿什么意思| 团长是什么军衔| pin是什么意思啊| 冬练三九夏练三伏是什么意思| 生肖鼠和什么生肖相冲| 宫颈锥切后需要注意什么| 座是什么结构| 抹茶是什么茶| 破绽是什么意思| 口痰多是什么原因| 梦到小鸟是什么意思| 三月份是什么星座的| 男人下面有异味什么原因| 男人做什么运动能提高性功能| 减肥挂什么科| 精子不液化吃什么药| 眩晕挂号挂什么科| 胃不舒服恶心想吐吃什么药| 尼莫地平片治什么病| 天煞孤星是什么意思| 体位是什么意思| 旗舰店是什么意思| 口腔医学专业学什么| 业力什么意思| 什么是结核病| 龙生九子是什么生肖| 女人喜欢什么姿势| 73年属什么| 男性内分泌失调有什么症状| 拉肚子吃什么食物比较好| 吹弹可破的意思是什么| 临床诊断是什么意思| 18k黄金是什么意思| 1996年属鼠五行属什么| 喝红茶有什么效果| 脖子淋巴结肿大是什么原因| 办身份证穿什么颜色衣服| 早上起来心慌是什么原因| 菊花代表什么象征意义| 孙俪什么学历| 佳木斯二院全名叫什么| 牛肉炒什么| 晚上手脚发热是什么原因| 追龙什么意思| 什么是唐卡| 善良对什么| 什么是自锁| 凉血是什么意思| 四肢冰凉是什么原因| 痉挛是什么意思| 县团委书记是什么级别| 女人为什么会患得患失| 胃不好吃什么水果| 女生隐私长什么样| 嘎嘎嘎是什么意思| 发什么什么大| 怀孕孕酮低有什么影响| 霜和乳有什么区别| 刚怀孕吃什么对胎儿好| 透疹是什么意思| 隐血阴性是什么意思| 阿飘是什么意思| 未加一笔是什么字| 金玉良缘什么意思| 1990年是什么年| 法不传六耳什么意思| 吃什么食物对肠胃好| 黄瓜不能和什么食物一起吃| 喝酸奶有什么好处| 六月生日是什么星座| 光纤和宽带有什么区别| 脂肪最怕什么| 指甲弯曲是什么原因| pcm是什么意思| 乙肝五项15阳性是什么意思| 什么是鸡皮肤图片| 白色糠疹是什么原因引起的| 倒闭是什么意思| 山大王是什么意思| 梦见包饺子是什么征兆| 小猫不能吃什么食物| 梦到捉鱼是什么意思| 黄水晶五行属什么| 阳气不足是什么意思| 送医生什么礼物好| 减肥可以吃什么水果| 你喜欢什么动物| 什么的脑袋| 木乃伊是什么| 梦见自己的头发长长了是什么意思| 为什么会气虚| 吐血是什么原因引起的| 女同是什么意思| 04年是什么年| 啤酒花是什么| 什么时候怀孕几率最高| 吃完避孕药有什么反应| 左右逢源是什么生肖| 羿字五行属什么| 盆腔炎要做什么检查| 91年属什么生肖| 六月份适合种什么蔬菜| 搞怪是什么意思| 过敏打什么针| 用什么泡脚可以脸上祛斑| 老想放屁是什么原因| 存款到100万算什么阶层| 人心惶惶是什么意思| 方法是什么意思| 弓山文念什么| gary什么意思| 为什么六月腊月不搬家| 老犯困是什么原因| 什么是脱肛| 什么是花胶| 运动后想吐是什么原因| 魔芋是什么东西| 大便白色是什么原因| 小蜘蛛吃什么| 什么是文员| 肾痛在什么位置痛| 大理有什么好玩的| 右脚浮肿预示着什么| 梦见流鼻血是什么征兆| 脑梗做什么检查| 审美观是什么意思| 2月22日什么星座| 峦读什么| 眼痒痒是什么原因引起| 蝌蚪吃什么| 女人喝什么补气养血| 大便有凹槽是什么原因| 梦见自己生二胎是什么意思| 对牛弹琴告诉我们什么道理| 做春梦是什么意思| 日加一笔可以变成什么字| wbc白细胞高是什么原因| 健康状况填什么| 什么叫卒中| 黑色五行属什么| 晚上尿多吃什么药| 国家电网是什么编制| 白带是什么样子| cup什么意思| 小孩咳嗽有痰吃什么药| 叶黄素对眼睛有什么好处| 酉时是什么时间| 窦性心动过速吃什么药| 人为什么打哈欠| 五马分尸是什么意思| 透骨草长什么样| 西游记主题曲叫什么| 良心是什么| 乙肝肝炎表面抗体阳性是什么意思| 红代表什么生肖| 珙桐是什么植物| 若干是什么意思| 喝水呛咳是什么原因| 骨结核吃什么药效果好| 中性粒细胞偏低是什么意思| 健康证什么时候可以办| 验孕棒一深一浅代表什么| 相恋纪念日送什么礼物| 艾滋病是什么症状| 安徽属于什么地区| 口苦口干口臭吃什么药| 花絮是什么意思| npn是什么意思| 这是什么踏板| 什么的什么的词语| 嘴臭是什么原因引起的| 为什么指甲会凹凸不平| 梦到小鸟是什么意思| 存款准备金率下调意味着什么| 界限性脑电图是什么意思| 胆囊炎输液用什么药| 草代表什么生肖| 梦见花开是什么预兆| 得宫颈癌的前兆是什么| 医生会诊是什么意思| 雨字五行属什么| 诺是什么意思| 杞菊地黄丸治什么病| 什么是催眠| 口臭口苦口干吃什么药| 喝牛奶就拉肚子是什么原因| 梦到高考是什么意思| 贞操是什么| 火棉胶婴儿是什么意思| 维生素b6有什么作用和功效| 吃什么减肥效果最好最快| 尿路感染看什么科室| 母婴传播是什么意思| 喝红茶有什么效果| 十羊九不全是什么意思| 吃什么可以降血压| 表姐的女儿叫什么| 述求是什么意思| 1993年出生的属什么| 脸上爱出汗是什么原因| 青海有什么湖| 公务员是干什么的| 化合物是什么| 吃什么会瘦| 生吃黄瓜有什么好处| 内分泌失调吃什么| 成林香是什么意思| 在什么| 女生胸部什么时候停止发育| 纯净水和矿泉水有什么区别| hardly什么意思| 萎缩性胃炎不能吃什么食物| 纠结是什么意思| 素色是什么颜色| 什么情况下会感染hpv病毒| 牵牛花为什么叫牵牛花| 人鱼线是什么| 89岁属什么生肖| 猫咪有泪痕是什么原因| 周杰伦为什么喜欢昆凌| 梦见骑自行车是什么意思| 渡人渡己什么意思| 换肾是什么病| 9.9号是什么星座| 武则天原名叫什么| 517是什么意思| 孕期阴道炎可以用什么药| 汽车抖动是什么原因| 83年猪是什么命| 百度Jump to content

完全掌握office2013高效办公超级手册光盘镜像版

From Wikibooks, open books for an open world
百度 出席本次对话会的中方主要嘉宾有:中国人民大学科研处处长、国家发展与战略研究院院长刘元春,国际交流处处长张晓京,国际关系学院教授王义桅,重阳金融研究院执行院长王文、信息中心总编辑胡海滨、国际合作项目主管杨凡欣等;德国著名非政府组织GIZ近年专门设立一带一路项目,项目主任、机构特别顾问阿斯特丽德.斯卡拉,新型市场可持续发展对话主任丹尼尔.塔拉斯专程从德国飞赴阿斯塔纳等。


Python includes a module for working with regular expressions on strings. For more information about writing regular expressions and syntax not specific to Python, see the regular expressions wikibook. Python's regular expression syntax is similar to Perl's

To start using regular expressions in your Python scripts, import the "re" module:

import re

Overview

[edit | edit source]

Regular expression functions in Python at a glance:

import re
if re.search("l+","Hello"):        print(1) # Substring match suffices
if not re.match("ell.","Hello"):   print(2) # The beginning of the string has to match
if re.match(".el","Hello"):        print(3)
if re.match("he..o","Hello",re.I): print(4) # Case-insensitive match
print(re.sub("l+", "l", "Hello"))           # Prints "Helo"; replacement AKA substitution
print(re.sub(r"(.*)\1", r"\1", "HeyHey"))   # Prints "Hey"; backreference
print(re.sub("EY", "ey", "HEy", flags=re.I))# Prints "Hey"; case-insensitive sub
print(re.sub(r"(?i)EY", r"ey", "HEy"))      # Prints "Hey"; case-insensitive sub
for match in re.findall("l+.", "Hello Dolly"):
  print(match)                              # Prints "llo" and then "lly"
for match in re.findall("e(l+.)", "Hello Dolly"):
  print(match)                              # Prints "llo"; match picks group 1
for match in re.findall("(l+)(.)", "Hello Dolly"):
  print(match[0], match[1])                 # The groups end up as items in a tuple
match = re.match("(Hello|Hi) (Tom|Thom)","Hello Tom Bombadil")
if match:                                 # Equivalent to if match is not None
  print(match.group(0))                   # Prints the whole match disregarding groups
  print(match.group(1) + match.group(2))  # Prints "HelloTom"

Matching and searching

[edit | edit source]

One of the most common uses for regular expressions is extracting a part of a string or testing for the existence of a pattern in a string. Python offers several functions to do this.

The match and search functions do mostly the same thing, except that the match function will only return a result if the pattern matches at the beginning of the string being searched, while search will find a match anywhere in the string.

>>> import re
>>> foo = re.compile(r'foo(.{,5})bar', re.I+re.S)
>>> st1 = 'Foo, Bar, Baz'
>>> st2 = '2. foo is bar'
>>> search1 = foo.search(st1)
>>> search2 = foo.search(st2)
>>> match1 = foo.match(st1)
>>> match2 = foo.match(st2)

In this example, match2 will be None, because the string st2 does not start with the given pattern. The other 3 results will be Match objects (see below).

You can also match and search without compiling a regexp:

>>> search3 = re.search('oo.*ba', st1, re.I)

Here we use the search function of the re module, rather than of the pattern object. For most cases, its best to compile the expression first. Not all of the re module functions support the flags argument and if the expression is used more than once, compiling first is more efficient and leads to cleaner looking code.

The compiled pattern object functions also have parameters for starting and ending the search, to search in a substring of the given string. In the first example in this section, match2 returns no result because the pattern does not start at the beginning of the string, but if we do:

>>> match3 = foo.match(st2, 3)

it works, because we tell it to start searching at character number 3 in the string.

What if we want to search for multiple instances of the pattern? Then we have two options. We can use the start and end position parameters of the search and match function in a loop, getting the position to start at from the previous match object (see below) or we can use the findall and finditer functions. The findall function returns a list of matching strings, useful for simple searching. For anything slightly complex, the finditer function should be used. This returns an iterator object, that when used in a loop, yields Match objects. For example:

>>> str3 = 'foo, Bar Foo. BAR FoO: bar'
>>> foo.findall(str3)
[', ', '. ', ': ']
>>> for match in foo.finditer(str3):
...     match.group(1)
...
', '
'. '
': '

If you're going to be iterating over the results of the search, using the finditer function is almost always a better choice.

Match objects

[edit | edit source]

Match objects are returned by the search and match functions, and include information about the pattern match.

The group function returns a string corresponding to a capture group (part of a regexp wrapped in ()) of the expression, or if no group number is given, the entire match. Using the search1 variable we defined above:

>>> search1.group()
'Foo, Bar'
>>> search1.group(1)
', '

Capture groups can also be given string names using a special syntax and referred to by matchobj.group('name'). For simple expressions this is unnecessary, but for more complex expressions it can be very useful.

You can also get the position of a match or a group in a string, using the start and end functions:

>>> search1.start()
0
>>> search1.end()
8
>>> search1.start(1)
3
>>> search1.end(1)
5

This returns the start and end locations of the entire match, and the start and end of the first (and in this case only) capture group, respectively.

Replacing

[edit | edit source]

Another use for regular expressions is replacing text in a string. To do this in Python, use the sub function.

sub takes up to 3 arguments: The text to replace with, the text to replace in, and, optionally, the maximum number of substitutions to make. Unlike the matching and searching functions, sub returns a string, consisting of the given text with the substitution(s) made.

>>> import re
>>> mystring = 'This string has a q in it'
>>> pattern = re.compile(r'(a[n]? )(\w) ')
>>> newstring = pattern.sub(r"\1'\2' ", mystring)
>>> newstring
"This string has a 'q' in it"

This takes any single alphanumeric character (\w in regular expression syntax) preceded by "a" or "an" and wraps in in single quotes. The \1 and \2 in the replacement string are backreferences to the 2 capture groups in the expression; these would be group(1) and group(2) on a Match object from a search.

The subn function is similar to sub, except it returns a tuple, consisting of the result string and the number of replacements made. Using the string and expression from before:

>>> subresult = pattern.subn(r"\1'\2' ", mystring)
>>> subresult
("This string has a 'q' in it", 1)

Replacing without constructing and compiling a pattern object:

>>> result = re.sub(r"b.*d","z","abccde")
>>> result
'aze'

Splitting

[edit | edit source]

The split function splits a string based on a given regular expression:

>>> import re
>>> mystring = '1. First part 2. Second part 3. Third part'
>>> re.split(r'\d\.', mystring)
['', ' First part ', ' Second part ', ' Third part']

Escaping

[edit | edit source]

The escape function escapes all non-alphanumeric characters in a string. This is useful if you need to take an unknown string that may contain regexp metacharacters like ( and . and create a regular expression from it.

>>> re.escape(r'This text (and this) must be escaped with a "\" to use in a regexp.')
'This\\ text\\ \\(and\\ this\\)\\ must\\ be\\ escaped\\ with\\ a\\ \\"\\\\\\"\\ to\\ use\\ in\\ a\\ regexp\\.'

Flags

[edit | edit source]

The different flags use with regular expressions:

Abbreviation Full name Description
re.I re.IGNORECASE Makes the regexp case-insensitive
re.L re.LOCALE Makes the behavior of some special sequences (\w, \W, \b, \B, \s, \S) dependent on the current locale
re.M re.MULTILINE Makes the ^ and $ characters match at the beginning and end of each line, rather than just the beginning and end of the string
re.S re.DOTALL Makes the . character match every character including newlines.
re.U re.UNICODE Makes \w, \W, \b, \B, \d, \D, \s, \S dependent on Unicode character properties
re.X re.VERBOSE Ignores whitespace except when in a character class or preceded by an non-escaped backslash, and ignores # (except when in a character class or preceded by an non-escaped backslash) and everything after it to the end of a line, so it can be used as a comment. This allows for cleaner-looking regexps.

Pattern objects

[edit | edit source]

If you're going to be using the same regexp more than once in a program, or if you just want to keep the regexps separated somehow, you should create a pattern object, and refer to it later when searching/replacing.

To create a pattern object, use the compile function.

import re
foo = re.compile(r'foo(.{,5})bar', re.I+re.S)

The first argument is the pattern, which matches the string "foo", followed by up to 5 of any character, then the string "bar", storing the middle characters to a group, which will be discussed later. The second, optional, argument is the flag or flags to modify the regexp's behavior. The flags themselves are simply variables referring to an integer used by the regular expression engine. In other languages, these would be constants, but Python does not have constants. Some of the regular expression functions do not support adding flags as a parameter when defining the pattern directly in the function, if you need any of the flags, it is best to use the compile function to create a pattern object.

The r preceding the expression string indicates that it should be treated as a raw string. This should normally be used when writing regexps, so that backslashes are interpreted literally rather than having to be escaped.

[edit | edit source]
荨麻疹能吃什么 耳石症是什么原因引起的 什么样的血管瘤不用治 正畸和矫正有什么区别 梦见别人买房子是什么预兆
什么水什么山 做肉丸用什么淀粉最佳 足贴为什么变黑出油 窝边草是什么意思 花中西施指的是什么花
粒字五行属什么 什么是扁平疣 一什么天空 为什么卧室要用木地板 拉油便是什么原因
犯口舌是什么意思 什么病 生吃苦瓜有什么好处和坏处 821是什么星座 2007属什么
什么空调最好hcv8jop6ns3r.cn 沙特用什么货币hcv9jop0ns5r.cn 鸟屎掉脸上有什么预兆hcv8jop5ns9r.cn 欣赏一个人是什么意思hcv8jop2ns0r.cn 暖味是什么意思hcv8jop5ns4r.cn
周海媚什么病hcv9jop5ns7r.cn 着重号是什么符号hcv9jop1ns6r.cn 周星驰为什么不结婚kuyehao.com 间歇性是什么意思hcv9jop0ns1r.cn 1992年属猴的是什么命hcv8jop4ns0r.cn
左手小手指麻木是什么原因引起的hcv9jop2ns1r.cn 小拇指发麻是什么原因hcv8jop4ns5r.cn 酪氨酸酶是什么东西hcv8jop6ns4r.cn 糗大了是什么意思hcv9jop6ns6r.cn 生理期腰疼是什么原因hcv8jop5ns0r.cn
耳鸣用什么药治疗效果最好jinxinzhichuang.com 赶的偏旁是什么hcv7jop9ns3r.cn 自闭是什么意思hcv9jop3ns0r.cn 晚上失眠是什么原因hcv8jop1ns1r.cn 滴虫性阴炎有什么症状表现hcv9jop1ns2r.cn
百度