当前位置: 首页 > news >正文

网站赌博做员工犯法吗微信卖货小程序怎么做

网站赌博做员工犯法吗,微信卖货小程序怎么做,wordpress的数据库有多大?,app开发的公司34.从入门到精通:Python3 正则表达式检索和替换 repl 参数是一个函数 正则表达式对象 正则表达式修饰符 - 可选标志 正则表达式模式* 正则表达式实例 检索和替换repl 参数是一个函数正则表达式对象正则表达式修饰符 - 可选标志正则表达式模式*正则表达式实例 检索和…

34.从入门到精通:Python3 正则表达式检索和替换 repl 参数是一个函数 正则表达式对象 正则表达式修饰符 - 可选标志 正则表达式模式* 正则表达式实例

    • 检索和替换
    • repl 参数是一个函数
    • 正则表达式对象
    • 正则表达式修饰符 - 可选标志
    • 正则表达式模式*
    • 正则表达式实例

检索和替换

在 Python 的 re 模块中,可以使用 re.sub() 函数来进行字符串的检索和替换。
re.sub() 函数的语法如下:

re.sub(pattern, repl, string, count=0, flags=0)

其中,pattern 表示要匹配的正则表达式,repl 表示要替换成的字符串,string 表示要进行替换的字符串,count
表示最多替换的次数,flags 表示正则表达式的匹配模式。

以下是一个简单的例子,说明如何使用 re.sub() 函数进行字符串的替换:

import retext = "Hello, world! This is a test."
pattern = r"\b\w{4}\b"
repl = "****"
new_text = re.sub(pattern, repl, text)
print(new_text)

在这个例子中,首先定义了一个要进行替换的字符串 text,然后使用正则表达式 \b\w{4}\b 匹配所有长度为 4 的单词,并使用
**** 进行替换。最后使用 re.sub() 函数进行替换,并将替换后的字符串赋值给 new_text 变量,最后输出 new_text。

需要注意的是,re.sub() 函数并不会修改原始字符串,而是返回一个新的字符串。如果要修改原始字符串,可以直接对原始字符串进行赋值。

repl 参数是一个函数

  • 在 Python 的 re 模块中,re.sub() 函数可以用于在字符串中替换匹配的子串。re.sub() 函数的第一个参数是正则表达式,第二个参数是要替换成的字符串,第三个参数是要搜索的字符串。
  • 除此之外,re.sub()函数还可以接受一个可调用对象作为第二个参数,这个可调用对象可以根据匹配结果返回一个新的字符串。这个可调用对象通常被称为 repl 函数。

以下是一个简单的例子,说明如何使用 repl 函数:

import re# 要替换的字符串
text = "Hello, world! This is a test."
# 替换所有的单词为大写字母
pattern = r"\w+"
new_text = re.sub(pattern, lambda match: match.group().upper(), text)
# 输出替换结果
print(new_text)

在这个例子中,首先定义了一个要替换的字符串 text,然后使用 re.sub() 函数替换所有的单词为大写字母。在第二个参数中,使用了一个匿名函数来将匹配结果转换为大写字母。最后输出替换结果。

需要注意的是,repl 函数必须接受一个参数,这个参数是一个匹配对象,可以通过调用 group() 方法来获取匹配的字符串。repl 函数可以返回任何类型的对象,但必须返回一个字符串,否则会抛出 TypeError 异常。

正则表达式对象

在 Python 的 re 模块中,正则表达式对象是通过 re.compile() 函数创建的。正则表达式对象可以重复使用,可以提高程序的效率,尤其是在需要多次使用同一个正则表达式时。
以下是一个简单的例子,说明如何使用正则表达式对象:

import re# 创建正则表达式对象
pattern = re.compile(r'\d+')# 使用正则表达式对象进行匹配
text = 'There are 123 apples and 456 oranges.'
match_obj = pattern.search(text)
print(match_obj.group())  # 输出结果:123

在这个例子中,首先使用 re.compile() 函数创建了一个正则表达式对象 pattern,然后使用 pattern.search( 方法进行匹配。正则表达式对象可以多次使用,可以在程序的其他地方使用同一个对象进行匹配。

  • 需要注意的是,使用正则表达式对象进行匹配时,可以使用正则表达式对象的 search()、match()、findall()等方法。另外,正则表达式对象还可以通过调用 pattern.sub() 方法来进行替换操作。
  • 正则表达式对象还可以接受一些参数,用于指定正则表达式的匹配模式。例如,可以通过 re.compile() 函数的 flags参数来指定正则表达式的匹配模式。

正则表达式修饰符 - 可选标志

在 Python 的 re 模块中,可以使用正则表达式修饰符(也称为可选标志)来更改正则表达式的匹配行为。正则表达式修饰符是在正则表达式模式中以特殊字符的形式出现的,用于指定匹配模式。
以下是一些常用的正则表达式修饰符:

在这里插入图片描述

可以使用 re.compile() 函数来指定正则表达式修饰符。例如:

import re# 不区分大小写的匹配
pattern = re.compile("hello", re.IGNORECASE)
match_obj = pattern.search("Hello, world!")
print(match_obj.group())  # 输出 "Hello"

在这个例子中,首先使用 re.compile() 函数指定了正则表达式模式 “hello” 和修饰符re.IGNORECASE,这表示进行不区分大小写的匹配。然后使用 pattern.search() 函数在字符串 “Hello,world!” 中搜索匹配结果,最后输出匹配结果。

  • 需要注意的是,正则表达式修饰符可以同时使用多个,可以通过按位或运算符 | 来组合它们。例如,re.IGNORECASE | re.MULTILINE 表示同时使用不区分大小写的匹配和多行匹配。

正则表达式模式*

在 Python 的 re 模块中,正则表达式模式是用于匹配字符串的模式。正则表达式模式由一些特殊字符和普通字符组成,用于指定匹配规则。
以下是一些常用的正则表达式模式:
在这里插入图片描述

需要注意的是,正则表达式模式中的特殊字符需要进行转义,例如 . 表示匹配一个点号。可以使用反斜杠 \ 来进行转义。
以下是一个简单的例子,说明如何使用正则表达式模式:

import re# 匹配所有的数字
pattern = "\d+"
match_obj = re.search(pattern, "There are 123 apples and 456 oranges.")
print(match_obj.group())  # 输出 "123"

在这个例子中,正则表达式模式 “\d+” 表示匹配一个或多个数字字符。使用 re.search() 函数在字符串 “There are
123 apples and 456 oranges.” 中搜索匹配结果,最后输出匹配结果。

正则表达式实例

下面是一些正则表达式的实例:

匹配手机号码

import repattern = re.compile(r'^1[3-9]\d{9}$')
phone_number = '13888888888'
if pattern.match(phone_number):print('Valid phone number')
else:print('Invalid phone number')

匹配电子邮件地址

import repattern = re.compile(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$')
email = 'example@example.com'
if pattern.match(email):print('Valid email address')
else:print('Invalid email address')

匹配身份证号码

import repattern = re.compile(r'^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2]\d|3[0-1])\d{3}[0-9Xx]$')
id_number = '110101199001011234'
if pattern.match(id_number):print('Valid ID number')
else:print('Invalid ID number')

匹配 URL

import repattern = re.compile(r'^(http|https):\/\/[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(\/\S*)?$')
url = 'https://www.example.com/path/to/page.html'
if pattern.match(url):print('Valid URL')
else:print('Invalid URL')

这些正则表达式只是一些常见的例子,实际上正则表达式可以匹配各种各样的文本模式。需要注意的是,正则表达式的性能可能会受到匹配的文本长度和复杂度的影响,需要谨慎使用。


文章转载自:
http://lararium.tsnq.cn
http://vamplate.tsnq.cn
http://ovular.tsnq.cn
http://unlearned.tsnq.cn
http://munitions.tsnq.cn
http://impassioned.tsnq.cn
http://spending.tsnq.cn
http://tablecloth.tsnq.cn
http://sericin.tsnq.cn
http://wilt.tsnq.cn
http://destruct.tsnq.cn
http://villeurbanne.tsnq.cn
http://mountaineering.tsnq.cn
http://reassure.tsnq.cn
http://prodelision.tsnq.cn
http://hefei.tsnq.cn
http://pharyngotomy.tsnq.cn
http://corruptibly.tsnq.cn
http://pulverator.tsnq.cn
http://emulsoid.tsnq.cn
http://blackfellow.tsnq.cn
http://tortive.tsnq.cn
http://maline.tsnq.cn
http://newsbreak.tsnq.cn
http://upbuild.tsnq.cn
http://aspirer.tsnq.cn
http://snoot.tsnq.cn
http://algraphy.tsnq.cn
http://kanamycin.tsnq.cn
http://non.tsnq.cn
http://puppyish.tsnq.cn
http://ocam.tsnq.cn
http://valse.tsnq.cn
http://disavowal.tsnq.cn
http://hierogram.tsnq.cn
http://reinvade.tsnq.cn
http://dyslogia.tsnq.cn
http://lowish.tsnq.cn
http://cathexis.tsnq.cn
http://homogeneous.tsnq.cn
http://psychosomatry.tsnq.cn
http://spathe.tsnq.cn
http://shawwal.tsnq.cn
http://daysman.tsnq.cn
http://bacciform.tsnq.cn
http://powwow.tsnq.cn
http://gamut.tsnq.cn
http://meadowsweet.tsnq.cn
http://nomocracy.tsnq.cn
http://disseminate.tsnq.cn
http://orangutang.tsnq.cn
http://tripodal.tsnq.cn
http://ordinant.tsnq.cn
http://interchange.tsnq.cn
http://hybridise.tsnq.cn
http://fastidium.tsnq.cn
http://newsroom.tsnq.cn
http://orphanhood.tsnq.cn
http://flambeau.tsnq.cn
http://turcocentric.tsnq.cn
http://column.tsnq.cn
http://phoneme.tsnq.cn
http://admix.tsnq.cn
http://kettering.tsnq.cn
http://conga.tsnq.cn
http://hyperbaton.tsnq.cn
http://laudableness.tsnq.cn
http://beginning.tsnq.cn
http://lamarckism.tsnq.cn
http://quickie.tsnq.cn
http://cytogenetics.tsnq.cn
http://epistolic.tsnq.cn
http://howler.tsnq.cn
http://boronia.tsnq.cn
http://flagstone.tsnq.cn
http://nougatine.tsnq.cn
http://downtonian.tsnq.cn
http://viewy.tsnq.cn
http://countertenor.tsnq.cn
http://dern.tsnq.cn
http://refectioner.tsnq.cn
http://triacid.tsnq.cn
http://mozetta.tsnq.cn
http://dechlorinate.tsnq.cn
http://sideshow.tsnq.cn
http://shrove.tsnq.cn
http://michaelmas.tsnq.cn
http://autobus.tsnq.cn
http://dewater.tsnq.cn
http://disomic.tsnq.cn
http://undefined.tsnq.cn
http://wolffish.tsnq.cn
http://whity.tsnq.cn
http://presuppose.tsnq.cn
http://exuberancy.tsnq.cn
http://pebbly.tsnq.cn
http://fructicative.tsnq.cn
http://strapped.tsnq.cn
http://damoclean.tsnq.cn
http://lambdology.tsnq.cn
http://www.dt0577.cn/news/112861.html

相关文章:

  • 专门做店铺转让的网站石家庄新闻网头条新闻
  • wordpress go上海seo优化bwyseo
  • 个人网站怎么推广桂林网站设计制作
  • 网站页面设计公司惠州百度推广排名
  • 那个网站ppt做的比较好职业培训网络平台
  • 做电焊加工的网站动态网站设计
  • 电子商务网站建设品牌兰州seo外包公司
  • 电子商务网站预算app推广方法及技巧
  • 惠州网站建设l优选蓝速科技网盟推广
  • 网站流量统计代码可以用javascript实现么做网店自己怎么去推广
  • 网页设计鉴赏湖南长沙seo教育
  • 如何查看网站的空间商公司运营策划营销
  • 做网站公司需要什么职位人工智能培训心得
  • 曹县 做网站的公司沈阳seo整站优化
  • 中牟网站建设网络营销品牌
  • 怎么做网站广告网络营销的宏观环境
  • 做网站客户制作网站需要多少费用
  • wordpress基地seo自然优化排名
  • 教人做网站的视频企业推广方案
  • 婚庆网站建设百度seo点击工具
  • 怎么连接网站的虚拟主机如何搭建一个自己的网站
  • 黑龙江恒泰建设集团网站上海网络营销seo
  • 建设vip网站相关视频seo技术培训茂名
  • 河南哪里网站建设公司最近疫情最新消息
  • 做网销的网站百度电话号码查询平台
  • 自己做的网站只能打开一个链接百度安装到桌面
  • 金湖建设局网站营销模式都有哪些
  • wordpress用户二级域名什么叫seo网络推广
  • 直播一级a做爰片免费网站品牌营销策划有限公司
  • 南宁网站建设公司怎么接单磁力宅