import re
string = "The quick brown fox jumps over the lazy dog."
a_list = string.split()
pattern = re.compile(r'THE',re.I)

count = 0
for word in a_list:
if pattern.search(word):
count +=1
print(count)

替换方式1

string_to_find = r"the"
pattern = re.compile(string_to_find,re.I)
print(re.sub(pattern,"a",string))

替换方式2

string_to_find = r"the"
pattern = re.compile(string_to_find,re.I)
print(pattern.sub("a",string))

re.sub索引替换作用,替换方式1和替换方式2的作用是一样的。

python re.I compile search的更多相关文章

  1. [LeetCode]题解(python):081 - Search in Rotated Sorted Array II

    题目来源 https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ Follow up for "Search in ...

  2. python JSON API duckduckgo search engine 使用duckduckgo API 尝试搜索引擎

    The duckduckgo.com's search engine is very neat to use. Acutally it has many things to do with other ...

  3. python语法re.compile模块介绍

    1. re模块是正则表达式模块,re模块中包含一个重要函数是compile(pattern [, flags]) ,该函数根据包含的正则表达式的字符串创建模式对象.可以实现更有效率的匹配. impor ...

  4. Learning Python 008 正则表达式-003 search()方法

    Python 正则表达式 - search()方法 findall()方法在找到第一个匹配之后,还会继续找下去,findall吗,就是找到所有的匹配的意思.如果你只是想找到第一个匹配的信息后,就不在继 ...

  5. Python之exec()/compile()方法使用

    # Python内置函数exec()可以用来执行Python代码 # 或内置函数compile()编译的代码对象 # exec程序中可写入python语法格式的代码,并直接输出. exec('prin ...

  6. [LeetCode&Python] Problem 704. Binary Search

    Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...

  7. python 带正则的search 模块

    glob  是python 提供的一个支持正则表达式的查找文件的模块. 实现上采用了os.listdir() 和 fnmatch.fnmatch().  但是没有真的invoking a subshe ...

  8. Python eval,exac,compile

    # eval 是把字符串类型的数据作为代码进行执行 s = "18+2" ret = eval(s) # 执行字符串类型的代码 print(ret) code = input(&q ...

  9. python eval, exec. compile

    compile 编译某段代码, (将一个字符串编译为字节代码), 以方便重复调用. exec 可以理解为和if, for一样是一个语法声明, 而不是一个函数. 注意globals和locals的含义. ...

随机推荐

  1. java 多线程实现的四种方式

    一个线程的生命周期 线程是一个动态执行的过程,它也有一个从产生到死亡的过程. 下图显示了一个线程完整的生命周期. 新建状态: 使用 new 关键字和 Thread 类或其子类建立一个线程对象后,该线程 ...

  2. postgres之清理空间碎片

    postgres=# select * from pg_stat_user_tables where relname = 'test'; -[ RECORD 1 ]-------+---------- ...

  3. C# 在Word表格中插入新行(表格含合并行)

    public string CreateWordFile(string CheckedInfo)         {             string message = "" ...

  4. leetcode-165周赛-1275-找出井字棋的获胜者

    题目描述: 自己的提交: class Solution: def tictactoe(self, moves: List[List[int]]) -> str: p = [[0] * 3 for ...

  5. 【和孩子一起学编程】 python笔记--第四天

    第十一章: 可变循环 newStars = int(input("how many stars do you want?")) for i in range(newStars): ...

  6. ContextLoaderListener vs DispatcherServlet

    In XML based Spring MVC configuration, you must have seen two declarations in web.xml file i.e. Cont ...

  7. Halo(三)

    接口中可以定义方法 1. 定义静态方法(直接调用) public interface Test { public static void method() { /** * 1.定义一个静态的带有方法体 ...

  8. 关于scrub的详细分析和建议

    https://ceph.com/planet/%E5%85%B3%E4%BA%8Escrub%E7%9A%84%E8%AF%A6%E7%BB%86%E5%88%86%E6%9E%90%E5%92%8 ...

  9. Vue项目中添加锁屏功能

    0. 直接上 预览链接 Vue项目中添加锁屏功能 1. 实现思路 ( 1 ) 设置锁屏密码 ( 2 ) 密码存localStorage (本项目已经封装h5的sessionStorage和localS ...

  10. Mysql全文索引的使用

    前言 在MySQL 5.6版本以前,只有MyISAM存储引擎支持全文引擎.在5.6版本中,InnoDB加入了对全文索引的支持,但是不支持中文全文索引.在5.7.6版本,MySQL内置了ngram全文解 ...