1.txt
imooc Javaimooc
imooc Htmlimooc
imooc Python imooc
C
C#
Go
VB
查找1.txt中以imooc开头的
查找1.txt中以imooc开头并以imooc结尾的
def find_start(fname):
f = open(fname)
for line in f:
if line.startswith('imooc'):
print line
def find_in(fname):
f = open(fname)
for line in f:
if line.startswith('imooc') and line[:-1].endswith('imooc'):
print line
find_in('1.txt')
判断一个变量是否是以下划线开头或以字母开头
a='_value'
print a and (a[0]=='_' or 'a'<=a[0]<='z')
正则表达式概念
1.使用单个字符串来描述匹配一系列符合某句语法规则的字符串
2.是对字符串操作的一种逻辑公式
3.应用场景:处理文本和数据
4.正则表达式过程:依次拿出表达式和文本中的字符比较,如果每一个字符都能
匹配,则匹配成功;否则匹配失败。
python第一个正则表达式
1.import re:python正则表达式模块
2.第一个正则表达式
re.compile(r’imooc’) pattern.mathh(‘imooc python)
r’imooc’ Pattern Match result
# -*- coding: utf-8 -*-
import re
pa = re.compile(r'imooc')
ma = pa.match('imooc python')
print ma.group()
print ma.span()#是位置
print ma.string#结果是字符串
print ma.re#结果是re.compile(r'imooc')