python 字符串替换、正则查找替换

import re

if __name__ == "__main__":
    url = "  \n   deded<a href = "">这是第一个链接</a><a href = "">这是第二个链接</a> \n      "
    # 去除\n
    one = url.replace("\n", "")
    # 去掉两端空格
    two = one.strip()
    # 正则匹配 re.match从字符串起始处匹配。
    three = re.match(r"(<a ([^>]*?)>)(.*?)(</a>)", two)
    if three is not None:
        arr = three.groups()
        print(arr[0] + "hahaha" + arr[3])

    # 正则查找是否含有</a>,re.search查找整个字符串,只要匹配即可。re.match从字符串开始查找,若开头没有匹配上则认为没有
    result = re.search("</a>", two).group()
    print(result)

    # 正则查找并替换
    print(re.sub(re.compile(r"<a.*?</a>", re.S), "", two))

原文地址:https://www.cnblogs.com/mydesky2012/p/9079476.html

时间: 2024-08-29 18:08:06

python 字符串替换、正则查找替换的相关文章

Linux Shell字符串操作(长度\查找\替换)详解

在做shell批处理程序时候,经常会涉及到字符串相关操作.有很多命令语句,如:awk,sed都可以做字符串各种操作. 其实shell内置一系列操作符号,可以达到类似效果,大家知道,使用内部操作符会省略启动外部程序等时间,因此速度会非常的快. 一.判断读取字符串值 表达式 含义 ${var} 变量var的值, 与$var相同 ${var-DEFAULT} 如果var没有被声明, 那么就以$DEFAULT作为其值 * ${var:-DEFAULT} 如果var没有被声明, 或者其值为空, 那么就以$

Python - 字符串模板的安全替换(safe_substitute) 具体解释

字符串模板的安全替换(safe_substitute) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27057339 字符串模板(sting.Template), 替换时, 使用substitute(), 未能提供模板所需的所有參数值时, 会发生异常. 假设使用safe_substitute(), 即安全替换, 则会替换存在的字典值, 保留未存在的替换符号. 代码: # -*- coding: utf-8 -*-

python字符串方法之查找

str.find() str.rfind() [作用:类似index,查找字符] [英语:r=>right|右边,find=>寻找] [说明:返回一个新的整数,查找到的位置,找不到出现-1,index找不到要报错] In [190]: "The stars are not afraid to appear like fireflies.".find("vition")#不存在返回-1 Out[190]: -1 In [192]: "The st

Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)

http://www.cnblogs.com/huangcong/archi s.strip() .lstrip() .rstrip(',') 去空格及特殊符号 复制字符串 Python 1 #strcpy(sStr1,sStr2) 2 sStr1 = 'strcpy' 3 sStr2 = sStr1 4 sStr1 = 'strcpy2' 5 print sStr2 连接字符串 Python 1 #strcat(sStr1,sStr2) 2 sStr1 = 'strcat' 3 sStr2 =

字符串 删除 比较 查找 替换 大小写转换 长度 创建 拼接 截取 前后缀

#import <Foundation/Foundation.h> #define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]) int main(int argc, const char * argv[]) { @autoreleasepool { NSString *str=[NSString stringWithForm

string.find(查找获取字符串) string.gsub(查找替换字符串) string.sub(截取字符串)

> aaa='/p/v2/api/winapi/adapter/lgj'> print(string.find(aaa, "^/.+/adapter/(.*)"))1 28 lgj > aaa='/p/v2/api/winapi/adapter/lgj/zy/l'> print(string.find(aaa, "^/.+/adapter/(.*)"))1 33 lgj/zy/l > print(string.gsub('lgj/zy/

20_Shell语言———VIM编辑器基础知识三之窗口属性定制、配置文件及查找替换功能

Vim编辑器可以让用户按照需求来定制一些使用属性. 一.窗口属性定义 1)显示行号 行号不是内容,只是用来帮助用户确认文本所在的行.在vim编辑器中,如果要显示行号,可以在末行模式下输入: set number 如果想关闭,则可以在功能名称前面加上no,即: set nonumber 命令可以被简写,如set number 可以简写为 set nu:set nonumber 可以简写为 set nonu. 注意,上述设定仅对当前vim的进程有效,一旦当前进程关闭,这些设定就会失效,如果要使设定永

python字符串搜索

python字符串字串查找 find和index方法 更多0 python 字符串 python 字符串查找有4个方法,1 find,2 index方法,3 rfind方法,4 rindex方法. 1 find()方法:查找子字符串,若找到返回从0开始的下标值,若找不到返回-1 info = 'abca'print info.find('a')##从下标0开始,查找在字符串里第一个出现的子串,返回结果:0 info = 'abca'print info.find('a',1)##从下标1开始,查

python字符串(大小写、判断、查找、分割、拼接、裁剪、替换、格式化)

一.通用操作 1.Python len() 方法返回对象(字符.列表.元组等)长度或项目个数. 语法 len()方法语法: len( q ) 参数 q -- 对象. 返回值 返回对象长度. 实例 以下实例展示了 len() 的使用方法: >>>str = "runoob" >>> len(str) # 字符串长度 6 >>> l = [1,2,3,4,5] >>> len(l) # 列表元素个数 5 2.pytho