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

怎样让自己做的网站被百度收录百度seo推广方案

怎样让自己做的网站被百度收录,百度seo推广方案,西安关键词网站排名,网页制作html完整代码一、前言 在字符串数据处理的过程中,正则表达式是我们经常使用到的,python中使用的则是re模块。下面会通过实际案例介绍 re.sub() 的详细用法,该函数主要用于替换字符串中的匹配项。 二、函数原型 首先从源代码来看一下该函数原型&#xf…

一、前言

在字符串数据处理的过程中,正则表达式是我们经常使用到的,python中使用的则是re模块。下面会通过实际案例介绍 re.sub() 的详细用法,该函数主要用于替换字符串中的匹配项

二、函数原型

首先从源代码来看一下该函数原型,包括各个参数及其意义:

	def sub(pattern, repl, string, count=0, flags=0):"""Return the string obtained by replacing the leftmostnon-overlapping occurrences of the pattern in string by thereplacement repl.  repl can be either a string or a callable;if a string, backslash escapes in it are processed.  If it isa callable, it's passed the match object and must returna replacement string to be used."""return _compile(pattern, flags).sub(repl, string, count)

从上面的代码中可以看到re.sub()方法中含有5个参数,下面进行一一说明(加粗的为必须参数):
(1)pattern:该参数表示正则中的模式字符串;
(2)repl:该参数表示要替换的字符串(即匹配到pattern后替换为repl),也可以是个函数;
(3)string:该参数表示要被处理(查找替换)的原始字符串;
(4)count:可选参数,表示是要替换的最大次数,而且必须是非负整数,该参数默认为0,即所有的匹配都会被替换;
(5)flags:可选参数,表示编译时用的匹配模式(如忽略大小写、多行模式等),数字形式,默认为0。

三、使用案例

下面将以一个字符串(包含大小写英文、数字、中英文标点、特殊符号等)作为示例进行使用案例讲解,该字符串如下:

>>> s = "大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn"

1.匹配单个数字或字母

(1)只匹配单一数字

>>> import re>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[0-9]', '*', s)"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m ** years old.   Today is ****/**/**. It is a wonderful DAY! @HHHHello,,,#***ComeHere***...**?AA?zz?——http://welcome.cn"

上面 re.sub(r'[0-9]', '*', s) 这句话则表示只匹配单一数字,并将每一个数字替换为一个星号 。

(2)只匹配单一字母

>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[a-z]', '*', s)"大家好,我是一个程序员小白。I '* ** **** ** ********* ******, *** I’* 18 ***** ***.   T**** ** 2020/01/01. I* ** * ********* DAY! @HHHH****,,,#111C***H***222...66?AA?**?——****://*******.**">>> re.sub(r'[A-Z]', '*', s)"大家好,我是一个程序员小白。* 'm so glad to introduce myself, and *’m 18 years old.   *oday is 2020/01/01. *t is a wonderful ***! @****ello,,,#111*ome*ere222...66?**?zz?——http://welcome.cn">>> re.sub(r'[A-Za-z]', '*', s)"大家好,我是一个程序员小白。* '* ** **** ** ********* ******, *** *’* 18 ***** ***.   ***** ** 2020/01/01. ** ** * ********* ***! @********,,,#111********222...66?**?**?——****://*******.**"

上面 re.sub(r'[a-z]', '*', s) 这句话则表示只匹配单一小写字母,并将每一个小写字母替换为一个星号
上面 re.sub(r'[A-Z]', '*', s) 这句话则表示只匹配单一大写字母,并将每一个大写字母替换为一个星号 。
上面 re.sub(r'[A-Za-z]', '*', s) 这句话则表示只匹配单一字母,并将每一个字母替换为一个星号 。

 (3)匹配单一数字和字母

>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[0-9A-Z]', '*', s)"大家好,我是一个程序员小白。* 'm so glad to introduce myself, and *’m ** years old.   *oday is ****/**/**. *t is a wonderful ***! @****ello,,,#****ome*ere***...**?**?zz?——http://welcome.cn">>> re.sub(r'[0-9a-z]', '*', s)"大家好,我是一个程序员小白。I '* ** **** ** ********* ******, *** I’* ** ***** ***.   T**** ** ****/**/**. I* ** * ********* DAY! @HHHH****,,,#***C***H******...**?AA?**?——****://*******.**">>> re.sub(r'[0-9A-Za-z]', '*', s)"大家好,我是一个程序员小白。* '* ** **** ** ********* ******, *** *’* ** ***** ***.   ***** ** ****/**/**. ** ** * ********* ***! @********,,,#**************...**?**?**?——****://*******.**"

上面 re.sub(r'[0-9A-Z]', '*', s) 这句话则表示只匹配单一数字和大写字母,并将每一个数字和大写字母替换为一个星号 。
上面 re.sub(r'[0-9a-z]', '*', s) 这句话则表示只匹配单一数字和小写字母,并将每一个数字和小写字母替换为一个星号 。
上面 re.sub(r'[0-9A-Za-z]', '*', s) 这句话则表示只匹配单一数字和字母,并将每一个数字和字母替换为一个星号 。

2.匹配多个数字或字母

注意:这里的所说的多个指的是大于等于一个。

(1)匹配多个数字

>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[0-9]+', '*', s)"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m * years old.   Today is */*/*. It is a wonderful DAY! @HHHHello,,,#*ComeHere*...*?AA?zz?——http://welcome.cn"

上面 re.sub(r'[0-9]+', '*', s) 这句话则表示匹配多个连续的数字,并将多个连续的数字替换为一个星号 。

(2)匹配多个字母

>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[a-z]+', '*', s)"大家好,我是一个程序员小白。I '* * * * * *, * I’* 18 * *.   T* * 2020/01/01. I* * * * DAY! @HHHH*,,,#111C*H*222...66?AA?*?——*://*.*">>> re.sub(r'[A-Z]+', '*', s)"大家好,我是一个程序员小白。* 'm so glad to introduce myself, and *’m 18 years old.   *oday is 2020/01/01. *t is a wonderful *! @*ello,,,#111*ome*ere222...66?*?zz?——http://welcome.cn">>> re.sub(r'[a-zA-Z]+', '*', s)"大家好,我是一个程序员小白。* '* * * * * *, * *’* 18 * *.   * * 2020/01/01. * * * * *! @*,,,#111*222...66?*?*?——*://*.*"

上面 re.sub(r'[a-z]+', '*', s) 这句话则表示匹配多个连续的小写字母,并将多个连续的小写字母替换为一个星号 。
上面 re.sub(r'[A-Z]+', '*', s) 这句话则表示匹配多个连续的大写字母,并将多个连续的大写字母替换为一个星号 。
上面 re.sub(r'[A-Za-z]+', '*', s) 这句话则表示匹配多个连续的字母,并将多个连续的字母替换为一个星号 。

(3)匹配多个数字和字母

>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[0-9a-zA-Z]+', '*', s)"大家好,我是一个程序员小白。* '* * * * * *, * *’* * * *.   * * */*/*. * * * * *! @*,,,#*...*?*?*?——*://*.*"

上面 re.sub(r'[0-9A-Za-z]+', '*', s) 这句话则表示匹配多个连续的数字和字母,并将多个连续的数字、连续的字母、连续的数字和字母替换为一个星号 。

3.匹配其他

(1)匹配非数字

>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[^0-9]', '*', s)'********************************************************18***********************2020*01*01**************************************111********222***66**************************'>>> re.sub(r'[^0-9]+', '*', s)'*18*2020*01*01*111*222*66*'

上面 re.sub(r'[^0-9]', '*', s) 这句话则表示匹配单个非数字,并将单个非数字替换为一个星号 。
上面 re.sub(r'[^0-9]+', '*', s) 这句话则表示匹配多个连续的非数字,并将多个连续的非数字替换为一个星号 。

(2)匹配非字母

>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[^a-z]', '*', s)'*****************m*so*glad*to*introduce*myself**and***m****years*old*****oday*is**************t*is*a*wonderful***********ello********ome*ere************zz***http***welcome*cn'>>> re.sub(r'[^A-Z]', '*', s)'**************I*************************************I*******************T********************I*****************DAY***HHHH***********C***H************AA***********************'>>> re.sub(r'[^A-Za-z]', '*', s)'**************I**m*so*glad*to*introduce*myself**and*I*m****years*old****Today*is*************It*is*a*wonderful*DAY***HHHHello*******ComeHere*********AA*zz***http***welcome*cn'>>> re.sub(r'[^a-z]+', '*', s)'*m*so*glad*to*introduce*myself*and*m*years*old*oday*is*t*is*a*wonderful*ello*ome*ere*zz*http*welcome*cn'>>> re.sub(r'[^A-Z]+', '*', s)'*I*I*T*I*DAY*HHHH*C*H*AA*'>>> re.sub(r'[^A-Za-z]+', '*', s)'*I*m*so*glad*to*introduce*myself*and*I*m*years*old*Today*is*It*is*a*wonderful*DAY*HHHHello*ComeHere*AA*zz*http*welcome*cn'

上面 re.sub(r'[^a-z]', '*', s) 这句话则表示匹配单个非小写字母,并将单个非小写字母替换为一个星号 。
上面 re.sub(r'[^A-Z]', '*', s) 这句话则表示匹配单个非大写字母,并将单个非大写字母替换为一个星号 。
上面 re.sub(r'[^A-Za-z]', '*', s) 这句话则表示匹配单个非字母,并将单个非字母替换为一个星号 。
上面 re.sub(r'[^a-z]+', '*', s) 这句话则表示匹配多个连续的非小写字母,并将多个连续的非小写字母替换为一个星号 。
上面 re.sub(r'[^A-Z]+', '*', s) 这句话则表示匹配多个连续的非大写字母,并将多个连续的非大写字母替换为一个星号 。
上面 re.sub(r'[^A-Za-z]+', '*', s) 这句话则表示匹配多个连续的非字母,并将多个连续的非字母替换为一个星号 。

(3)匹配非数字和非字母

>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[^0-9A-Za-z]', '*', s)'**************I**m*so*glad*to*introduce*myself**and*I*m*18*years*old****Today*is*2020*01*01**It*is*a*wonderful*DAY***HHHHello****111ComeHere222***66*AA*zz***http***welcome*cn'>>> re.sub(r'[^0-9A-Za-z]+', '*', s)'*I*m*so*glad*to*introduce*myself*and*I*m*18*years*old*Today*is*2020*01*01*It*is*a*wonderful*DAY*HHHHello*111ComeHere222*66*AA*zz*http*welcome*cn'

上面 re.sub(r'[^0-9A-Za-z]', '*', s) 这句话则表示匹配单个非数字和非字母,并将单个非数字和非字母替换为一个星号 。
上面 re.sub(r'[^0-9A-Za-z]+', '*', s) 这句话则表示匹配多个连续的非数字和非字母,并将多个连续的非数字和非字母替换为一个星号 。

(4)匹配固定形式

a.只保留字母和空格,将 repl 设置为空字符即可。

	>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[^a-z ]', '', s)' m so glad to introduce myself and m  years old   oday is  t is a wonderful  elloomeerezzhttpwelcomecn'>>> re.sub(r'[^a-z ]+', '', s)' m so glad to introduce myself and m  years old   oday is  t is a wonderful  elloomeerezzhttpwelcomecn'>>> re.sub(r'[^A-Za-z ]', '', s)'I m so glad to introduce myself and Im  years old   Today is  It is a wonderful DAY HHHHelloComeHereAAzzhttpwelcomecn'>>> re.sub(r'[^A-Za-z ]+', '', s)'I m so glad to introduce myself and Im  years old   Today is  It is a wonderful DAY HHHHelloComeHereAAzzhttpwelcomecn'

如果要使句子语义和结构更完整,则要先将其余字符替换为空格(即repl设置为空格),然后去除多余的空格,如下:

>>> s1 = re.sub(r'[^A-Za-z ]+', ' ', s)>>> s1' I  m so glad to introduce myself  and I m   years old    Today is   It is a wonderful DAY   HHHHello ComeHere AA zz http welcome cn'>>> re.sub(r'[ ]+', ' ', s1)' I m so glad to introduce myself and I m years old Today is It is a wonderful DAY HHHHello ComeHere AA zz http welcome cn'

 b.去除以 @ 开头的英文单词

>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'@[A-Za-z]+', '', s)"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! ,,,#111ComeHere222...66?AA?zz?——http://welcome.cn"

c.去除以 ?结尾的英文单词和数字(注意这是中文问号)

>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'[A-Za-z]+?', '', s)"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?——http://welcome.cn">>> re.sub(r'[0-9A-Za-z]+?', '', s)"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...——http://welcome.cn"

d.去除原始字符串中的URL

>>> s"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——http://welcome.cn">>> re.sub(r'http[:.]+\S+', '', s)"大家好,我是一个程序员小白。I 'm so glad to introduce myself, and I’m 18 years old.   Today is 2020/01/01. It is a wonderful DAY! @HHHHello,,,#111ComeHere222...66?AA?zz?——"

文章来源于https://blog.csdn.net/jackandsnow/article/details/103885422

http://www.hkea.cn/news/627992/

相关文章:

  • 北京公司响应式网站建设价位太原网站建设开发
  • 龙岩网络诈骗通缉人员优化推广方案
  • 专做海外代购的网站设计案例网
  • 漳州网站设计东莞网站制作模板
  • 制作应用的软件成都排名seo公司
  • 北京专业企业网站建设俄罗斯搜索引擎入口 yandex
  • wordpress 怎么改中文网站推广优化价格
  • 南山做网站公司怎么选择企业seo优化服务
  • 什么 电子商务网站建设与管百度招商加盟
  • 南召微网站开发手机优化大师官方版
  • 营销型网站技术特点网站推广网
  • 龙游县住房和城乡建设局网站百度seo优化方法
  • 深圳方维网站建设设计个人网站
  • wordpress 流量站百度应用
  • ps素材网seo在线工具
  • 岳阳网站开发公司html网站模板免费
  • 怎样用模板做网站优化网站技术
  • 全国新型疫情最新情况长沙网站搭建优化
  • 郑州网站建设规划seo建站教程
  • 购物网站 购物车界面如何做百度搜索网
  • 推广网站的图片怎么做外贸平台
  • 新手如何给自己的网站做优化bt种子磁力搜索
  • 成都学校网站制作遵义网站seo
  • d?t网站模版宁波seo在线优化哪家好
  • c做的网站淄博做网站的公司
  • 网站开发制作公司郑州网站建设外包
  • 注册域名用个人还是公司好长沙seo优化排名
  • 电子商务网站建设与维护展望今日新闻联播
  • 网站建设主流技术站长之家ping检测
  • 温州建设集团有限公司网站首页百度手机版网页