40 python 正则表达式 match方法匹配字符串 使用search函数在一个字符串中查找子字

第一课: 使用match方法匹配字符串
# 正则表达式:使用match方法匹配字符串

‘‘‘
正则表达式:是用来处理文本的,将一组类似的字符串进行抽象,形成的文本模式字符串

windows dir *.txt file1.txt file2.txt abc.txt test.doc
a-file1.txt-b
linux/mac ls

主要是学会正则表达式的5方面的方法
1. match:检测字符串是否匹配正则表达式
2. search:在一个长的字符串中搜索匹配正则表达式的子字符串
3. findall:查找字符串
4. sub和subn:搜索和替换
5. split: 通过正则表达式指定分隔符,通过这个分隔符将字符串拆分
‘‘‘

import re # 导入正则表达的模块的

m = re.match(‘hello‘, ‘hello‘) # 第一个指定正则表达式的字符串,第二个表示待匹配的字符串 实际上 正则表达式也可以是一个简单的字符串

print(m) # <re.Match object; span=(0, 5), match=‘hello‘>

print(m.__class__.__name__) # 看一下m的类型 Match
print(type(m)) # <class ‘re.Match‘>

m = re.match(‘hello‘, ‘world‘)
if m is not None:
print(‘匹配成功‘)
else:
print(‘匹配不成功‘)

# 待匹配的字符串的包含的话正则表达式,系统也认为是不匹配的
m = re.match(‘hello‘, ‘world hello‘)
print(m) # None 如果不匹配的话,返回值为None

# 待匹配的字符串的前缀可以匹配正则表达式,系统也认为是匹配的
m = re.match(‘hello‘, ‘hello world‘)
print(m) # <re.Match object; span=(0, 5), match=‘hello‘>

----------------------------------------------
第二课 正则中使用search函数在一个字符串中查找子字符串

# search函数 和 match函数的区别是,search函数中 字符串存在就可以匹配到,match函数 必须要 前缀可以匹配正则表达式,系统也认为是匹配的

import re

m = re.match(‘python‘, ‘I love python.‘)
if m is not None:
print(m.group()) # 调用group的方法就是返回到一个组里面
print(m) # None
m = re.search(‘python‘, ‘I love python.‘)
if m is not None:
print(m.group())
print(m)
#
# python
# <re.Match object; span=(7, 13), match=‘python‘>
span=(7, 13) 表示 python在 ‘I love python.‘ 字符串中的位置
左闭右开

原文地址:https://blog.51cto.com/12445535/2465940

时间: 2024-08-04 03:31:53

40 python 正则表达式 match方法匹配字符串 使用search函数在一个字符串中查找子字的相关文章

3.05 在一个表中查找与其他表不匹配的记录

问题:对于具有相同关键字的两个表,要在一个表中查找与另外一个不匹配的行.例如,要查找没有职员的部门. 要查找部门中每个员工的工作岗位需要在表需要在表deptno及emp中有一个等值联接.deptno列就是这两个表之间的公共值.但是,等值联接却不能直接显示出那个部门没有员工.这是因为在表emp和dept正在等值联接时,将会返回满足联接条件的所有行.可是我们只需要那些在表dept中不满足联接条件的行. 尽管乍看起来这个问题同前一个问题类似,但是要更复杂一些.其不同之处就是在前一个问题中,需要列出在表

在父字符串中查找子字符串

在父字符串中查找子字符串(指针控制,也可选择标控制) #pragma once #include<iostream> #include<assert.h> using namespace std; char* StrStr(char* source, char* dest) { assert(source&&dest); if (strlen(source) < strlen(dest)) return NULL; char* newSrc = NULL; c

js 判断字符串是否包含某字符串,String对象中查找子字符,indexOf

1 var Cts = "bblText"; 2 3 if(Cts.indexOf("Text") > 0 ) 4 { 5 alert('Cts中包含Text字符串'); 6 } indexOf用法:  返回 String 对象内第一次出现子字符串的字符位置.       strObj.indexOf(subString[, startIndex])       参数    strObj       必选项.String 对象或文字.       subStr

JQUERY选择和操作DOM元素(利用正则表达式的方法匹配字符串中的一部分)

1.匹配属性的开头 $("[attributeName^='value']"); 2.匹配属性的结尾 $("[attributeName$='value']"); 3.属性选择器总结 elem[attr] 选择具有attr属性的元素 elem[attr=val]选择具有attr属性且属性值与val值匹配的元素 elem[attr^=valu]选择具有attr属性且属性值以val值开头的元素 elem[attr$=val]选择具有attr属性且属性值以val值结尾的元

python正则表达式3-模式匹配

re.S,使 '.'  匹配换行在内的所有字符 >>> pattern=r'ghostwu.com' >>> import re >>> re.findall( pattern, 'ghostwuacom' ) ['ghostwuacom'] >>> re.findall( pattern, 'ghostwubcom' ) ['ghostwubcom'] >>> re.findall( pattern, 'ghost

Python正则表达式返回首次匹配到的字符及查询的健壮性

re.findall(pattern,string)会搜索所有匹配的字符,返回的是一个列表,获取首个匹配需要re.findall(pattern,string)[0]访问, 但是如果findall没匹配成功则返回空列表,这时用列表下标去访问元素时就会报IndexError: list index out of range. 如: >>>re.findall('abc','abd') [] >>>re.findall('abc','abd')[0] Traceback (

实现字符串检索strstr函数、字符串长度strlen函数、字符串拷贝strcpy函数

1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 /* 6 _Check_return_ _Ret_maybenull_ 7 inline char* __CRTDECL strstr(_In_z_ char* const _String, _In_z_ char const* const _SubString) 8 { 9 return const_cast<char*>

python在一个列表中查找

# -*- coding: utf-8 -*-__author__ = 'Administrator'import bisect#简化一些操作#1:在一个列表中查找"""说明:需要在一个列表的实例中提供一个搜索算法,对该列表的已排序版本执行二分查找就可以将复杂度从o(n)降至o(log n)"""def listfind(seq,el):    pos=bisect.bisect(seq,el)    if pos==0 or (pos==len

.net正则表达式大全(.net 的 System.Text.RegularExpressions.Regex.Match()方法使用)

正则表达式的本质是使用一系列特殊字符模式,来表示某一类字符串.正则表达式无疑是处理文本最有力的工具,而.NET的System.dll类库提供的System.Text.RegularExpressions.Regex类实现了验证正则表达式的方法.Regex 类表示不可变(只读)的正则表达式.它还包含各种静态方法,允许在不显式创建其他类的实例的情况下使用其他正则表达式类. 正则表达式的字符代表的说明: Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN